Generics is a feature of runtime.Generics allow classes, structs, interfaces, delegates, and methods
There are two distinct collection types in C#.
The standard collections, which are found under the System.Collections namespace and the generic collections,
under System.Collections.Generic.
The generic collections are more flexible and are the preferred way to work with data.
The generic collections or generics were introduced in .NET framework 2.0. Generics enhance code reuse, type safety, and performance.
System.Collections.Generic
The System.Collections.Generic namespace contains several generic collection classes based on generics
The namespace contains a lot of generic classes, structures, and interfaces like
Dictionary
List
Queue
SortedDictionary
Stack
public class Gen<T>
{
T t;
public T Val
{
get;
set;
}
}
The class name " Gen<T” represents that the Type is generic, specifically the brackets <>containing the Type. This Type "T" is used to show that if we need to refer to the actual Type that is going to be used when we write this class".
T t; Creates member of the type T
Type safe code can never access any private members of an object
Advantages
much more reusable.
No runtime Casting No Boxing hence Allows Performance Efficiency
EX:Using generic collections now, this pretty much goes away.
If you create a List<int>, there is no boxing done - the List<int> can hold the integers directly.
Creates Type safe at compile time as generics specify type at runtime
The stronger type checking associated with generics helps in finding errors easily at compile time only.
No code Duplication
Advantages are assured type safety, enhanced performance, readability.
DisAdvantages - Make things little complex
--------------------------------------------------------------------------------
Is .NET Type-Safe?
A type error is erroneous or undesirable program behaviour caused by a discrepancy between differing data types. […] The behaviors classified as type errors by a given programming language are usually those that result from attempts to perform operations on values that are not of the appropriate data type.
There are two distinct collection types in C#.
The standard collections, which are found under the System.Collections namespace and the generic collections,
under System.Collections.Generic.
The generic collections are more flexible and are the preferred way to work with data.
The generic collections or generics were introduced in .NET framework 2.0. Generics enhance code reuse, type safety, and performance.
System.Collections.Generic
The System.Collections.Generic namespace contains several generic collection classes based on generics
The namespace contains a lot of generic classes, structures, and interfaces like
Dictionary
List
Queue
SortedDictionary
Stack
public class Gen<T>
{
T t;
public T Val
{
get;
set;
}
}
The class name " Gen<T” represents that the Type is generic, specifically the brackets <>containing the Type. This Type "T" is used to show that if we need to refer to the actual Type that is going to be used when we write this class".
T t; Creates member of the type T
Type safe code can never access any private members of an object
Advantages
much more reusable.
No runtime Casting No Boxing hence Allows Performance Efficiency
EX:Using generic collections now, this pretty much goes away.
If you create a List<int>, there is no boxing done - the List<int> can hold the integers directly.
Creates Type safe at compile time as generics specify type at runtime
The stronger type checking associated with generics helps in finding errors easily at compile time only.
No code Duplication
Advantages are assured type safety, enhanced performance, readability.
DisAdvantages - Make things little complex
--------------------------------------------------------------------------------
Is .NET Type-Safe?
A type error is erroneous or undesirable program behaviour caused by a discrepancy between differing data types. […] The behaviors classified as type errors by a given programming language are usually those that result from attempts to perform operations on values that are not of the appropriate data type.
Comments
Post a Comment