Generic subclassing, type parameters and delegates in C#

Goud.Kv
Posted by in C# category on for Intermediate level | Points: 250 | Views : 7212 red flag

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Recommendation
Read Generic constraints with examples in C# before this article.

Introduction

In the previous article, we have studied about generic constraints of C# with examples. Now, let's see generic subclassing, type parameters and delegates in this chapter.

Objective

The main objective of this article is to learn generic subclassing, generic type parameters and generic delegates in C# programming.

Generic Subclassing

We can subclass a generic class like we do for non-generic class. Take a look at the below example in which the subclass can leave the base class's type parameter as open.
class MyClass<T>
{
   .........;
}

class MySecnd<T> : MyClass<T>
{
    ...........;
    ..........;
}
Or it can close the parameters of generic types with concrete types. For example,
class MyClass<T>
{
    ..........;
}

class MyInt : MyClass<int>
{
    ..........;
    .........;
}
We can also declare the subtypes with with the type arguments like below,
class MyClass<T>
{
    ..........;
}

class SubClass<T,TKey> : MyClass<T>
{
    ..........;
    .........;
}

Generic Type Parameters

Type parameter is a place holder for that type in which the client specifies when they instantiate a variable of the generic type.
For example, let's take GenericList<T>. In order to use this, client code must declare a constructed type and need to specify a type argument inside the braces (<>) to instantiate that constructed type.
We can use any type as the type argument but that must be recognized by the compiler.

Example,
GenericList<int> MyList = new GenericList<int>();
In order to use a class as type argument, we should follow the below approach,
GenericList<MyClass> Test = new GenericList<MyClass>();
For structs also it is same like above, i.e,
GenericList<MyStruct> Exam = new GenericList<MyStruct>();

Generic Delegates


What is delegate?
Delegate is the C#'s keyword that helps to make method objects. It is often used for event handlers to specify the handler type.

Let's have a simple example of using delegate in C# programming,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example
{
    class Program
    {
        delegate void Name(string name);
        
        static void Main()
        {
            // Using lambda expression to specify the delegate
            Name myname = m => Console.WriteLine(m);
 
            myname.Invoke("Krishna");         // Invoking the delegate
        }
    }
}
In the above example, we have a delegate Name(). In the Main() method, we have created instance to the delegate and specified it by using the lambda expression.

Finally, invoking the delegate will calls the method Name() and compiles it to print the following lines in your console,


What is a lambda expression?
Lambda expressions provides an easy and simple way to specify the methods. It is little complex to represent the syntax for delegate functions. Hence lambda expressions are used.
In this, separation of parameters and the method body is done by using '=>' syntax.

Note: Lambda can be passed to the method that receives an Action, Func, Predicate or Comparision.

Generic delegates

Like we did with generic class and generic interface, generic delegate can specify the type argument to create a closed constructed type.

Syntax,
public delegate void MyDel<T>(T prop); // Generic delegate declaration
public static void Product(int i)
{
    ........;
    .......;
}
And we can create instance to that delegate like below,
MyDel<int> d = new MyDel<int>(Product);
There is a new feature called method group conversion, which is applicable for generic as well as concrete delegate types.
It allows us to write the above line like below,
MyDel<int> d2 = Product;

Conclusion

In this article, we have studied with generic subclassing, generic type parameters and delegates in C#. Hope you understand.

Thanks for reading,

Regards,
Krishna.

Recommendation
Read Variance in generic types of C# after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Goud.Kv
Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)