What are Generics?
- The System.Collections.Generics namespace contains the generics collections.
- Generics can be a method, class, structure, or an interface to which it acts upon
- Generics can be refer to a method ,class, structure or an interface in a type – safe manner
- Generics provide the ability to create type-safe collections in .NET.
- Generics.Net is a well organized class which contains data structures, algorithms, design patterns and other utilities in generic form.
- Generics are used to make the code more reusable.
- Generics can act like any data type means there is no need to write any internal code.
What are Constraints in Generics?
- Constraints are represented in C# using the where keyword
- Constraints is something restriction the way doing that in generics
- There are 3 types of Constraints
1. Constructor constraint
2. Derivation constraint
3. Reference / value type constraints
What are Generics and Casting?
- The C# compiler only lets you implicitly cast generic type parameters to Object, or to constraint-specified types
- The compiler explicitly cast generic type parameters to only an interface but not a class.
How to use I Dispose of a Generic Type?
- Just pass value of generic type parameter to the using statement then compiler will allow you to specify any instance of generic type
How to declare Generic Methods?
- Generic method is defined by specifying the type parameter after the method name but before the parameter list.
- Doe Example public string Add
(T val1, T val2).
What is use of Generic List?
- Generic List is an efficient method of storing a collection of variables
- Generic List is strongly typed and casting.
- Name space to declare generics is using System.Collections.Generic.
Difference between Generics and Array List?
- Array List is not type safe because it faces problems of boxing and UN boxing.
- List generics are type safe and do not require boxing and UN boxing situations.
- In terms of performance of application List generics is better than array list.
- Array List can save different type data types.
- List generics we can save only specific data type.
- Array List consumes lots of memory compare to list generics/