Home > Back-end >  Why assign a method to a variable in C#?
Why assign a method to a variable in C#?

Time:04-15

I see code written like this:

static void Main(string[] args)
{
    var name = GetInfo(); //this variable contains the method below
    Console.Writeline(name)
}

static string GetInfo()
{
    string name = "MyName";
    return (name);
}

Let's use the typical box example. What's a good example for method inside it?

In other words, what does it mean 'a value' = 'a method that does something'

CodePudding user response:

Its not assigning a method, it is just getting the result of that executed method. In this way you can reuse GetInfo(); in other places where ever you need this name.

  • Related