C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
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
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,
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.