<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ravi Sankar Rao</title>
    <description>The latest articles on Forem by Ravi Sankar Rao (@ravisankarrao).</description>
    <link>https://forem.com/ravisankarrao</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F794084%2Fa0a97d62-4f47-454f-8ac2-35010252ebdb.jpg</url>
      <title>Forem: Ravi Sankar Rao</title>
      <link>https://forem.com/ravisankarrao</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ravisankarrao"/>
    <language>en</language>
    <item>
      <title>C# Delegates Simplified — Part 3</title>
      <dc:creator>Ravi Sankar Rao</dc:creator>
      <pubDate>Thu, 14 Sep 2023 05:35:18 +0000</pubDate>
      <link>https://forem.com/ravisankarrao/c-delegates-simplified-part-iii-2gb0</link>
      <guid>https://forem.com/ravisankarrao/c-delegates-simplified-part-iii-2gb0</guid>
      <description>&lt;p&gt;&lt;em&gt;This article is the third one from a series of 3 articles on C# delegates. If you are new to delegates and haven’t gone through the previous ones, I suggest you to go through them first&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;a href="https://dev.to/ravisankarrao/c-delegates-simplified-part-1-5bnf"&gt;Part 1&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;&lt;a href="https://dev.to/ravisankarrao/c-delegates-simplified-part-2-3cmm"&gt;Part 2&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We all might have read articles that say “If you are using lambda expressions in C#, you are already working with delegates” but they don’t discuss anything about how they are closely related.&lt;/p&gt;

&lt;p&gt;In this article we will discuss the journey from delegate to lambda expressions.&lt;/p&gt;

&lt;p&gt;Below we have a person class with two properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Person
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have a function somewhere outside this class which takes a Person parameter and returns formatted name&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public string GetFullName(Person person)
{
       return $"{person.FirstName} {person.LastName}";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets declare a delegate for the function which should match the signature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public delegate string PersonFormatter(Person person);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using above delegate with our function will be like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var person = new Person { FirstName = "John", LastName = "Doe" };

PersonFormatter formatter = GetFullName;
var fullName = formatter(person);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have a separate function for our delegate, but for most of our day-to-day programming we don’t need to write a separate function for our delegate to be used.&lt;/p&gt;

&lt;p&gt;We can achieve use a delegate by using something known as &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/anonymous-functions"&gt;Anonymous functions&lt;/a&gt; in C#. Below is how we can do that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PersonFormatter formatter = delegate (Person person)
{
       return $"{person.FirstName} {person.LastName}";
};

// No changes in the way delegate is invoked
var fullName = formatter(person);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax has 4 major parts&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;a variable named formatter of type &lt;em&gt;PersonFormatter&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;use of &lt;strong&gt;delegate&lt;/strong&gt; keyword&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Person&lt;/strong&gt; variable in round brackets&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;a function body that does the same as our &lt;em&gt;GetFullName&lt;/em&gt; function earlier. This function body does not has a name, hence an Anonymous function.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can simplify the use of anonymous function while we initialize our delegate. We can get rid of the ‘delegate’ keyword and use ‘=&amp;gt;’ which is commonly knows as ‘goes to’ operator or famously in our case as the ‘&lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-operator"&gt;lambda&lt;/a&gt;’ operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PersonFormatter formatter = (Person person) =&amp;gt;
{
        return $"{person.FirstName} {person.LastName}";
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We removed the delegate keyword, and added a lambda operator after our Person variable.&lt;/p&gt;

&lt;p&gt;We can simplify this even more. Taking advantage of C#’s &lt;strong&gt;Type Inference&lt;/strong&gt;, we can also get rid of the Person type and just use a parameter name as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PersonFormatter formatter = (person) =&amp;gt;
{
        return $"{person.FirstName} {person.LastName}";
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We got rid of the type &lt;em&gt;Person&lt;/em&gt;, and are just using person as a parameter . C#’s Type inference automatically kicks in due to the delegate type &lt;em&gt;PersonFormatter&lt;/em&gt; that the parameter it needs is of Person type.&lt;/p&gt;

&lt;p&gt;Interesting till now ? More on the way.&lt;/p&gt;

&lt;p&gt;We can simplify this even further. It happens, if we are working with single parameter delegates, we don’t even need the brackets around the parameter as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PersonFormatter formatter = person =&amp;gt;
{
        return $"{person.FirstName} {person.LastName}";
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below, I have renamed person to ‘p’ for simplification.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PersonFormatter formatter = p =&amp;gt;
{
        return $"{p.FirstName} {p.LastName}";
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what we call as “&lt;strong&gt;Statement Lambdas&lt;/strong&gt;”, meaning expressions are enclosed in braces.&lt;/p&gt;

&lt;p&gt;We can simplify this even further. If our anonymous function body has only one statement, we can get rid of curly brackets and the return keyword as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PersonFormatter formatter = p =&amp;gt; $"{p.FirstName}{p.LastName}";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called as “&lt;strong&gt;Expression Lambdas&lt;/strong&gt;”&lt;/p&gt;

&lt;p&gt;If we are working with more that one parameters, we have to use brackets around our parameter&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public delegate int DoSomeMath(int a, int b);

// Below is to add two numbers
DoSomeMath someMath = (a, b) =&amp;gt; a + b;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Hope the articles helped to bridge the gap between delegates and lambda expressions.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>dotnetcore</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# Delegates Simplified — Part 2</title>
      <dc:creator>Ravi Sankar Rao</dc:creator>
      <pubDate>Thu, 14 Sep 2023 05:23:31 +0000</pubDate>
      <link>https://forem.com/ravisankarrao/c-delegates-simplified-part-2-3cmm</link>
      <guid>https://forem.com/ravisankarrao/c-delegates-simplified-part-2-3cmm</guid>
      <description>&lt;p&gt;&lt;em&gt;This article is the second one of a series of 3 articles on C# delegates. If you haven’t gone through the &lt;a href="https://dev.to/ravisankarrao/c-delegates-simplified-part-1-5bnf"&gt;part 1&lt;/a&gt;, I suggest you to go through it first.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/ravisankarrao/c-delegates-simplified-part-1-5bnf"&gt;previous article&lt;/a&gt; we went through a brief intro on what delegates are, how are they declared, initialized and invoked. Here we will take a simple use case where delegates can be useful.&lt;/p&gt;

&lt;p&gt;One thing which the previous article did not mention is that, we use delegates majorly to pass functions as parameters. We will work on this usage here.&lt;/p&gt;

&lt;p&gt;We have a Person class with FirstName and LastName as its member properties. The requirement is to get a person’s name in different formats. For one of the case, we have overridden &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.object.tostring?view=netcore-3.1#the-default-objecttostring-method"&gt;ToString&lt;/a&gt; as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Person
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public override string ToString()
        {
            return $"{this.FirstName} {this.LastName}";
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I created a small WPF application to demonstrate this. Screenshot of the UI is below. It has a set of radio buttons to select a format, then on click of “Display Name” button, names should be displayed within the list box on the right as per the selected format radio button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fU3tu39G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kk5jk621gz19j84dg8ru.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fU3tu39G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kk5jk621gz19j84dg8ru.png" alt="Image description" width="691" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Coming back to our Person class. In order for the class to support different formats of name, one way we can achieve this is by creating a parameterized &lt;em&gt;ToString&lt;/em&gt; method in the class, send some kind of &lt;em&gt;enum&lt;/em&gt; to decide in which format we need to return, add a switch case, construct that format and return the formatted name. Below is how this can be done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public enum FormatType
{
        Default = 1,
        LastNameFirst,
        LastNameOnly,
        FirstNameOnly,
        InitialsOnly
}

public class Person
{
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString()
        {
            return $"{this.FirstName} {this.LastName}";
        }

        public string ToString(FormatType format)
        {
            switch (format)
            {
                case FormatType.Default:
                    return this.ToString();
                case FormatType.LastNameFirst:
                    return $"{this.LastName} {this.FirstName}";
                case FormatType.LastNameOnly:
                    return this.LastName;
                case FormatType.FirstNameOnly:
                    return this.FirstName;
            }
            return default;
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Though above design would work fine but there is a problem with that. Every time we need a new format to be added, we need modify Person’s &lt;em&gt;ToString&lt;/em&gt; method to add one more case for a new format type.&lt;/p&gt;

&lt;p&gt;This goes against the &lt;strong&gt;Open-Close&lt;/strong&gt; principle of SOLID design which states that a software entity (Person class in this case) should be open for extension but closed for modification.&lt;/p&gt;

&lt;p&gt;This also goes against &lt;strong&gt;Single Responsibility&lt;/strong&gt; principle of SOLID which states an entity should have responsibility for a single part of the program. In this case the responsibility of Person class should only be of holding the data, and not what to do with this data.&lt;/p&gt;

&lt;p&gt;We can address both the issues using delegates&lt;/p&gt;

&lt;p&gt;Below is a modified Person class that uses a &lt;em&gt;ToString&lt;/em&gt; method that takes a delegate as a parameter and returns a string. We invoke the delegate inside this method and return its result, in this case it being string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public delegate string PersonFormatter(Person persion);
public class Person
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string ToString(PersonFormatter formatter)
        {
            return formatter(this);
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see how the Person class is now lean and does not do anything apart from storing information.&lt;/p&gt;

&lt;p&gt;We create a &lt;em&gt;NameFormatter&lt;/em&gt; class with different methods to format the name. This was we keep the name formatting code separate from Person class. It has four methods as per the radio buttons in our WPF UI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static class NameFormatter
{
        public static string Default(Person person)
        {
            return $"{person.FirstName} {person.LastName}";
        }
        public static string LastNameFirst(Person person)
        {
            return $"{person.LastName} {person.FirstName}";
        }
        public static string FirstNameOnly(Person person)
        {
            return person.FirstName;
        }
        public static string LastNameOnly(Person person)
        {
            return person.LastName;
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember the WPF app screenshot above ? In the code behind file, we have the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;List of persons which is dummy data — people&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Local variable of type &lt;em&gt;PersonFormatter&lt;/em&gt; delegate — formatter&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Function which assigns the above formatter variable based on which radio button is selected.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List&amp;lt;Person&amp;gt; people = new List&amp;lt;Person&amp;gt;
{
            new Person { FirstName = "John", LastName = "Doe" },
            new Person { FirstName = "Tony", LastName = "Stark" }
};

public PersonFormatter formatter;

public void AssignFormatter()
{
            if (rdBtn_Default.IsChecked.Value)
                formatter = NameFormatter.Default;
            else if (rdBtn_LastNameFirst.IsChecked.Value)
                formatter = NameFormatter.LastNameFirst;
            else if (rdBtn_FirstNameOnly.IsChecked.Value)
                formatter = NameFormatter.FirstNameOnly;
            else if (rdBtn_LastNameOnly.IsChecked.Value)
                formatter = NameFormatter.LastNameOnly;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On click of the “Display Names” button, below function is invoked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private void DisplayName_Click(object sender, RoutedEventArgs e)
{
            namesList.Items.Clear();   // clearing list box
            AssignFormatter();        // based on radio button  
            foreach (var person in people)
                namesList.Items.Add(person.ToString(formatter));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We clear all items from the &lt;em&gt;listbox&lt;/em&gt; named as &lt;em&gt;namesList&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;AssignFormatter&lt;/em&gt; function is invoked which assigns the type of formatter function to be assigned to our delegate variable formatter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We iterate through people, and for each of the item we call it’s &lt;em&gt;ToString&lt;/em&gt; method and pass our delegate formatter. If you remember, we had a &lt;em&gt;ToString&lt;/em&gt; method which takes &lt;a href="https://dev.tourl"&gt;PersonFormatter&lt;/a&gt; delegate as a parameter&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public string ToString(PersonFormatter formatter)
{
       return formatter(this);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the formatter is invoked which in turn invokes which ever NameFormatter function was assigned earlier in the AssignFormatter based on selected radio button. The result is returned as string.&lt;/p&gt;

&lt;p&gt;This example is a fairly simple use of delegates but gives more of an idea how they are useful to follow SOLID design principles.&lt;/p&gt;

&lt;p&gt;In the next article we discuss how delegates are related to lambda expressions.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>dotnetcore</category>
      <category>programming</category>
    </item>
    <item>
      <title>C# Delegates Simplified — Part 1</title>
      <dc:creator>Ravi Sankar Rao</dc:creator>
      <pubDate>Thu, 14 Sep 2023 05:11:37 +0000</pubDate>
      <link>https://forem.com/ravisankarrao/c-delegates-simplified-part-1-5bnf</link>
      <guid>https://forem.com/ravisankarrao/c-delegates-simplified-part-1-5bnf</guid>
      <description>&lt;p&gt;&lt;em&gt;This article is the first of a series of 3 articles on C# delegates.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example we will be working with
&lt;/h2&gt;

&lt;p&gt;Before directly jumping to delegates, lets take a look at the example. We have a static Maths class with two functions — FindSquare and FindSqrRoot. static here does not signify anything. It’s used just to simplify the course of this article.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static class Maths
{
        public static double FindSquare(double number)
        {
            return number * number;
        }
        public static double FindSqrRoot(double number)
        {
            return Math.Sqrt(number);
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both functions take a single parameter of type double, and both return double. Note that the signature of both methods is same because this will come into picture later.&lt;/p&gt;

&lt;p&gt;We invoke these methods as per our need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;double number = 25;

Maths.FindSqrRoot(number);
Maths.FindSquare(number);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Coming to Delegates
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;If we go by the definition given in &lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/"&gt;C# docs&lt;/a&gt;, A delegate is a type that represents &lt;em&gt;references to methods with a particular parameter list and return type&lt;/em&gt;. To simplify it — a delegate is a type that &lt;em&gt;&lt;strong&gt;defines a method signature&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do we declare a delegate?
&lt;/h3&gt;

&lt;p&gt;It is declared almost similar to a function except that it does not has a statement body for itself. It has a return type and might take parameters if required exactly like functions. Below is an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// No return type, No parameters
public delegate void MyDelegate();

// Has a return type, takes parameters
public delegate int MyDelegate(int someValue);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our example, for the two methods if we declare a delegate, it will be as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public delegate double CalculationsWithSingleNumber(double number);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you compare this with our two Maths functions, you will notice the signature is exactly the same, i.e. the return type and parameters are same.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using a Delegate
&lt;/h3&gt;

&lt;p&gt;Using a delegate is very simple too. We declare a variable of our delegate type and assign a function reference as it’s value. The delegate variable acts now as a function itself. We can invoke it just like a function, send our number and get the required result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CalculationsWithSingleNumber doSomeMath = Maths.FindSquare;
double result = doSomeMath(10); // returns the square of the given number

doSomeMath = Maths.FindSqrRoot;
result = doSomeMath(25); // returns the square root of the given number
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can assign a function to a delegate which has the exact same signature. &lt;br&gt;
Below is an example of function with two parameters. Trying to assign it to our delegate will result in compilation error with message saying — &lt;strong&gt;&lt;em&gt;No overload for  matches delegate &lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static double AddNumbers(double a, double b)
{
     return a + b;
}

// This cannot be done since AddNumbers takes 2 parameters and the delegate takes only 1
CalculationsWithSingleNumber doSomeMoreMath = Maths.AddNumbers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We saw how a delegate is declared, assigned and invoked, but one question that comes to mind is how we significantly use delegates in our programming.&lt;/p&gt;

&lt;p&gt;Please visit article part 2 of this series for a simple example and implementation.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>dotnetcore</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
