简体   繁体   中英

C# delegates hiding

I'm learning Delegates. I simply writing 2 functions that returns a string each and then i hide them inside a delegate like this:

    delegate string PrinterDelegate();
class Printer
{
    public PrinterDelegate BlackColor = PrintBlackColor;
    public PrinterDelegate Color = PrintColor;

    private static string PrintBlackColor()
    {
        return "Printing black color";
    }
    private static string PrintColor()
    {
        return "Printing with color";
    }

    public string Returner(Func<string> func)
    {
        return func();
    }
}

(I will say what Returner is after i will ask my question) so the program.cs looks like this:

Printer printer = new Printer();
Func<string> mainPrinter = () => { return printer.BlackColor(); };
string totogate = printer.Returner(mainPrinter);
Console.WriteLine(totogate);

and of course i get:
"Printing black color" in the console.
now what i'm not able to understand is this line:

Func<string> mainPrinter = () => { return printer.BlackColor(); };

What does it mean to return a PrinterDelegate named BlackColor under mainPrinter which is Func? what does mainPrinter hold?
and when i pass mainPrinter into Returner, what is it that returns into the string name totogate?
i know it will return the "Printing black color", but how?
What happens exactly?

Breaking it down:

Func<string> mainPrinter = () => { return printer.BlackColor(); };
  • Func<string> mainPrinter declares a variable that has the type Func<string>
  • = assigns it eh the value () => { return printer.BlackColor(); } () => { return printer.BlackColor(); }
  • () => { return printer.BlackColor(); } () => { return printer.BlackColor(); } is the interesting part and is lambda syntax to declare an anonymous function that takes no arguments returns a string. The compiler looks at the return statement to infer its return type.

Altogether the statement declares a delegate for a function with no arguments that returns a string (see Func<TResult> ). The delegate is assigned to a reference to the anonymous function declared as {return printer.BlackColor;} .

You told:

I'm learning Delegates.

Ok, and this is the specific of Func<string>:

public delegate TResult Func<out TResult>();

Exactly, Func is a Delegate. So, from MSDN:

A delegate is a type that represents references to methods with a particular parameter list and return type.

Your Func represents a reference to a methods with no parameter list and return a string. You can think like a variable that can store a func instead of int or string:

Func<string> mainPrinter = () => { return printer.BlackColor(); };

mainPrinter is a variable that store a function, () => { return printer.BlackColor(); } function.

So, like read a int variable, you can execute a Func variable ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM