C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Introduction
So far, we have studied
delegates vs interfaces in the previous chapter. We are going to learn different compatibles related with
delegates in this chapter.
Objective
The main objective of this article is to learn about type compatibility and parameter compatibility of
delegates in
C# programming.
Compatibility of Delegates
Let's see different types of compatibles with delegates one by one below.
Type Compatibility
All the delegate types are incompatible with each other even though they have the same signatures.
Example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
class Program
{
delegate void Del1(); // Delegate 1
delegate void Del2(); // Delegate 2
static void MyMethod()
{
Console.WriteLine("This is MyMethod");
}
static void Main()
{
Del1 one = MyMethod;
// Del2 two = one; // Not possible
one.Invoke(); // prints the text "This is MyMethod"
Del2 two = new Del2(one); // This is possible
two.Invoke(); // prints the text "This is MyMethod"
}
}
}
In the above code, we have two delegates
Del1()
and
Del2()
.
In the Main() method, we have created an instance of Del1()
(i.e. one
) and referred it to MyMethod()
.
It is not possible to convert the types implicitly like below,
Del1 one = MyMethod;
Del2 two = one // Compile time error
Hence, all the
delegate types are incompatible with each other.
We can do the explicit conversion like below,
Del2 two = new Del2(one); // possible
Now, invoking "
two
" prints the following lines in the console as it is explicitly referenced to "
one
".
If both the delegates are targeted to the same method, then they are considered equal.
For easy understanding, we are doing a slight modification in the above code which is,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
class Program
{
delegate void Del1(); // Delegate 1
delegate void Del2(); // Delegate 2
static void MyMethod() // static method
{
Console.WriteLine("This is MyMethod");
}
static void Main()
{
Del1 myDel1 = MyMethod;
Del1 myDel2 = MyMethod;
Console.WriteLine(myDel1 == myDel2); // prints True
}
}
}
In the above code, we are pointing both the instances '
myDel1
' and '
myDel2
' to
MyMethod()
. This tells that both the instances are considered as equal.
If you run this code, it will print "True" in your console like below,
Parameter Compatibility
Example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
delegate void MyAction(string act); // MyAction delegate
class Program
{
static void ActionObject(object obj) // static method
{
Console.WriteLine(obj);
}
static void Main()
{
MyAction action = new MyAction(ActionObject); // Delegate instance referenced to ActionObject
action("This is my action");
}
}
}
In the above example, we have a delegate
MyAction()
with
string "
act
" as its parameter. In the
Program class, we have a static method
ActionObject() with
object "
obj
" as parameter.
Now, the whole purpose of this code is to check the parameter compatibility. In the Main() method, delegate instance "action
" is referenced to the ActionObject which will invokes the same at the next line.
In this program, MyAction
is invoked with an argument of type string. And that is implicitly upcasted to the object while relayed to the target method. This means delegates are parameter compatible.
Above code prints the following result in your console,
Note: Delegates are variant only for reference conversions in case of type parameter variance.
Conclusion
In this article, we have studied type compatibility and parameter compatibility of delegates in
C# programming. Hope you understand.
Thanks for reading,
Regards,
Krishna