Action <> a few syntax explanations of the parameters

Sometimes I canโ€™t understand the simplest things, Iโ€™m sure itโ€™s on my face, I just donโ€™t see it. I am trying to create a delegate for a method in this simple class:

public static class BalloonTip
{
    public static BalloonType BalType
    { 
        get; 
        set; 
    }

    public static void ShowBalloon(string message, BalloonType bType)
    {
        // notify user
    }
}

Now this Action <> should create a delegate without declaring the keyword "delegate", did I understand correctly? Then:

private void NotifyUser(string message, BalloonTip.BalloonType ballType)
    {
        Action<string, BalloonTip.BalloonType> act; 
        act((message, ballType) => BalloonTip.ShowBalloon(message,  ballType));
    }

This does not compile. What for?

(By the way, the reason I need this delegate instead of calling ShowBalloon () directly is that the calls should be made from a thread other than the interface, so I decided that I needed the <> action)

Thank,

+5
source share
2 answers

Action, , :

private void NotifyUser(string message, BalloonTip.BalloonType ballType)
{
    Action<string, BalloonTip.BalloonType> act = 
        (m, b) => BalloonTip.ShowBalloon(m, b);

    act(message, ballType);
}

, , Action, , :

private void NotifyUser(string message, BalloonTip.BalloonType ballType)
{
    Action<string, BalloonTip.BalloonType> act = BalloonTip.ShowBalloon;

    act(message, ballType);
}
+9

act? - :

Action<string, BalloonTip.BalloonType> act = BalloonTip.ShowBalloon;

act, , , , BalloonTip.BalloonType.
act, , , :

public Action<string, BalloonTip.BalloonType> GetNotificationMethod() {
   Action<string, BalloonTip.BalloonType> act = BalloonTip.ShowBalloon;
   return act;
}  

:

public Action<string, BalloonTip.BalloonType> GetNotificationMethod() {
   return BalloonTip.ShowBalloon;
}  

, . .

+2

All Articles