Hello All, We are going to start new batch from next week. message/call or mail us for more details.

Generics in C#


Generics in C#

Generics are the most powerful feature of C# 2.0.
 Generics allow you to define type-safe data structures, without committing to actual data types. 
This results in a significant performance boost and higher quality code, because you get to reuse data processing algorithms without duplicating type-specific code. In concept, generics are similar to C++ templates, but are drastically different in implementation and capabilities. This article discusses the problem space generics address, how they are implemented, the benefits of the programming model, and unique innovations, such as constrains, generic methods and delegates, and generic inheritance. You will also see how generics are utilized in other areas of the .NET Framework such as reflection, arrays, collections, serialization, and remoting, and how to improve on the basic offering.
Generics were added to version 2.0 of the C# language and the common language runtime (CLR). Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter T you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations, as shown here:
C#

// Declare the generic class.
public class GenericList
{
    void Add(T input) { }
}
class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int.
        GenericList<int> list1 = new GenericList<int>();

        // Declare a list of type string.
        GenericList<string> list2 = new GenericList<string>();

        // Declare a list of type ExampleClass.
        GenericList list3 = new GenericList();
    }
}


We can refer to a class, where we don't force it to be related to any specific Type, but we can still perform work with it in a Type-Safe manner. A perfect example of where we would need Generics is in dealing with collections of items (integers, strings, Orders etc.). We can create a generic collection than can handle any Type in a generic and Type-Safe manner.

Generics allow us to create type safe collections with no boxing and un-boxing overhead. It is a concept that allows us to achieve parametric polymorphism.

 

What Are Generics

Generics allow you to define type-safe classes without compromising type safety, performance, or productivity. You implement the server only once as a generic server, while at the same time you can declare and use it with any type. To do that, use the <and > brackets, enclosing a generic type parameter.

Generic Constraints

With C# generics, the compiler compiles the generic code into IL independent of any type arguments that the clients will use. As a result, the generic code could try to use methods, properties, or members of the generic type parameters that are incompatible with the specific type arguments the client uses. This is unacceptable because it amounts to lack of type safety. In C# you need to instruct the compiler which constraints the client-specified types must obey in order for them to be used instead of the generic type parameters. There are three types of constraints. A derivation constraint indicates to the compiler that the generic type parameter derives from a base type such an interface or a particular base class. A default constructor constraint indicates to the compiler that the generic type parameter exposes a default public constructor (a public constructor with no parameters). A reference/value type constraint constrains the generic type parameter to be a reference or a value type. A generic type can employ multiple constraints, and you even get IntelliSense reflecting the constraints when using the generic type parameter, such as suggesting methods or members from the base type.
It is important to note that although constraints are optional, they are often essential when developing a generic type. Without them, the compiler takes the more conservative, type-safe approach and only allows access to Object-level functionality in your generic type parameters. Constraints are part of the generic type metadata so that the client-side compiler can take advantage of them as well. The client-side compiler only allows the client developer to use types that comply with the constraints, thus enforcing type safety. 

·         Use generic types to maximize code reuse, type safety, and performance.
·         The most common use of generics is to create collection classes.
·         The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible instead of classes such as ArrayList in the System.Collections namespace.
·         You can create your own generic interfaces, classes, methods, events and delegates.
·         Generic classes may be constrained to enable access to methods on particular data types.
·         Information on the types that are used in a generic data type may be obtained at run-time by using reflection.
Why Generics?
  1. Generics let us to specify our Type at runtime. you can create a collection that is type-safe at compile-time.
  2. Gives no boxing, no casting advantage.
  3. Both the casting and the boxing and unboxing operations degrade performance.
  4. Use of list collection as their new type not as objects.
  5. Binary code reuse
  6. Code clearness with generics is another advantage. 

3 comments:

  1. Hi,

    Here are some useful links for the interview.

    https://www.logicsmeet.com/forum/WCF/52822-WCF-interview-questions-and-answers-for-experienced.aspx

    https://www.logicsmeet.com/forum/SQL%20Server/73056-sql-server-interview-questions-and-answers-for-experienced.aspx

    Thanks,

    ReplyDelete
  2. It is very good blog and useful for students and developer , Thanks for sharing

    Dot Net Online Training Hyderabad

    ReplyDelete