Search This Blog

Saturday, April 12, 2014

The Concept Of Generics In .Net With C# And VB.Net

The Detailed Description Of Generics In .Net

Generic Type : A generic type is a type that uses generic type parameters. For example, the type LinkedList, defined as:

C#
public class LinkedList
{...}
VB.Net
Public Class LinkedList(Of K, T)
    ...
End Class

Generic Type Parameter : A generic type parameter is the place holder a generic type uses. For example, the generic type LinkedList, defined as:

C#
public class LinkedList
{...}
VB.Net
Public Class LinkedList(Of K, T)
    ...
End Class

Some more description about generics:
Constructed Type : A constructed type is any generic type that has at least one type argument.

Open Constructed Type : A open constructed type is any generic type that which contains at least one type parameter used as a type argument

Closed Constructed Type : A closed constructed type is a generic type that which contains no type parameters as type arguments.

Initialize a Generic Type Parameter

When declaring a generic type, you need to specify the types that will replace the type parameters in the declaration. These are known as type arguments to the generic type. Type arguments are simply types. 
You need to specify which types to use for K, the list's key, and T, the data items stored in the list. You specify the types in two places: when declaring the list's variable and when instantiating it:
C#
LinkedList list = new LinkedList();
list.AddHead(123,"ABC");
VB.Net
Dim list As New LinkedList(Of Integer, String)
list.AddHead(123, "ABC")

Constraints in Generics:

Constraints allow additional contextual information to be added to the type parameters of generic types. The constraints limit the range of types that are allowed to be used as type arguments, but at the same time, they add information about those type parameters. Constraints ensure that the type arguments specified by the client code are compatible with the generic type parameters the generic type itself uses. Meaning, constraints prevent the client from specifying types as type arguments that do not offer the methods, properties, or members of the generic type parameters that the generic type relies upon.
After applying a constraint you get IntelliSense reflecting the constraints when using the generic type parameter, such as suggesting methods or members from the base type.
Derivation constraint indicates to the compiler that the generic type parameters derives from a base type such an interface or a particular base class. 
You can provide constraints for every generic type parameter that your class declares. You can have a base class constraint, meaning, stipulating that the generic type parameter derives from a particular base class:
However, you can only use one base class at most in a constraint because neither C#, Visual Basic or managed C++ support multiple inheritance of implementation. Obviously, the base class you constrain to cannot be a sealed class, and the compiler enforces that. In addition, you cannot constrain System.Delegate or System.Array as a base class.
You can constrain both a base class and one or more interfaces, but the base class must appear first in the derivation constraint list.You can combine the default constructor constraint with derivation constraints, provided the default constructor constraint appears last in the constraint list.

No comments:

Post a Comment