Delegates in C#- Part 1

Delegates in C#- Part 1

Delegates- (Namespace: System.Delegate)

A beginners guide to delegates with examples

Delegates- (Namespace: System.Delegate)

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-orientedtype-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:

Question 1- How does one declare and instantiate a delegate?

Question 2-How does one associate methods to it?

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.

Instantiating a delegate & associating one or more methods to it.

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);

Image for post

Function and Delegate Signature should be identical.

We then call the methods:

Console.WriteLine(delegate1(10,29));

Console.WriteLine(delegate2(25,2500));

Image for post

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).

Multicast Delegate

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.

Image for post

coding-interviews programming dotnet c-sharp-programming interview

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

5 Best C# (C-Sharp) Programming Courses for Beginners in 2020

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 programming for Beginners | Learn C Programming | C Language

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.

How to Prepare for a Coding Interview in 8 Weeks

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.

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

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.

Classes and Objects in C++ | C++ Programming Language - Part2

This Edureka video Classes and Objects in C++ will help you to get started with C++.