Delegates- (Namespace: System.Delegate)
A beginners guide to delegates with examples
Definition: *A delegate is a type that safely *encapsulates a method, similar to a function pointer in C and C++. Unlike function pointers, delegates are object-oriented, type-safe and secure. The type of delegate is defined by the name of the delegate.
(The above definition is from** MSDN**.)
OR
A delegate is a reference type that holds the reference to a method or group of methods
Now for a beginner, some of these terminologies might be strange and one might have several questions in one’s mind :
·Question 1- How does one declare and instantiate a delegate?
·Question 2- How does one associate methods to it?
·Question 3- Where are these delegates used in the real world and have I ever come across them knowingly or unknowingly?
Let us get to the first two questions:
Syntax : delegate
Declaration:- public delegate int MyDelegate (string s);
“MyDelegate” here is a delegate which accepts a string as a parameter and returns an integer. The delegate and the methods it wishes to refer must have identical signatures.
A delegate is instantiated by using the new keyword and associated with a method.
Example: public delegate int NumberTailor(int x, int y);
This is how we instantiate it:
var delegate1 = new NumberTailor(DelegateHelpers.AddNumbers);
var delegate2 = new NumberTailor(DelegateHelpers.MultiplyNumbers);
Function and Delegate Signature should be identical.
We then call the methods:
Console.WriteLine(delegate1(10,29));
Console.WriteLine(delegate2(25,2500));
Calling of methods via delegate and its output.
As mentioned earlier in the declaration section, a delegate can be used to call any method matching its signature, however, the function must strictly adhere to its signature or else it throws a compilation error (example shown later).
We will now look at an example where we can add references to more than one method to the delegate.
class DelegateHelpers
{
public static string TamperWithEvidence(string evidence)
{
var tamperedEvidence = string.Empty;
if (evidence.ToLower().Contains(“evidence”))
{
tamperedEvidence = evidence.Replace(“evidence”, “***d****”);
}
return tamperedEvidence;
}
public static string DoNotTamperWithEvidence(string evidence)
{
return string.Empty;
}
}
The code above is of the two methods we shall refer through our delegate. Notice here, DoNotTamperWithEvidence(string) *method always returns an empty string, whilst *TamperWithEvidence(string) returns a tampered evidence string.
class Program
{
public delegate string StringTailor(string str);
static void Main(string[] args)
{
var stringTailor = new StringTailor(DelegateHelpers.TamperWithEvidence);
//stringTailor += DelegateHelpers.DoNotTamperWithEvidence;
var oldString = “The was evidence against him.”;
var newString = stringTailor(oldString);
Console.WriteLine(“old string = “ + oldString
+ “\nnew string = “ + newString
);
}
}
The code above is our main method wherein we call our methods via the delegates. A thing to notice here is that we have commented out the line where we add a new method reference to our delegate. Let us look at the output of the same.
coding-interviews programming dotnet c-sharp-programming interview
In this article, I am going to share some of the best courses to learn C# or C-Sharp in 2020. These courses are suitable for both beginners and experienced programmers who want to fill gaps in their C# knowledge.
C Language is an evergreen language and is used widely across different industries, This C programming is a must for students and working professionals to become a great Software Engineer especially when they are working in Software Development Domain. Great Learning brings you this live session on "Introduction to C". In this live session, we will be covering major concepts in C Programming such as Different Variables, Different Data Types that are being used, its Operators, Flow control statements, Structure, and lot more.
As of this writing, the market is tough. We’ve been hit hard with a deadly pandemic that left thousands of people unemployed. It’s layoffs everywhere and the companies are being conservative when it comes to hiring.
C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.
This Edureka video Classes and Objects in C++ will help you to get started with C++.