Created
February 14, 2013 20:38
-
-
Save yetanotherchris/4956191 to your computer and use it in GitHub Desktop.
Action<T> examples in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ActionExample | |
{ | |
private List<string> _names = new List<string>(); | |
private delegate void myDelegate(string s); | |
private void AddName(string name) | |
{ | |
_names.Add(name); | |
} | |
public void Run() | |
{ | |
// The delegate equivalent just uses void instead of string. | |
Action<string> actionMethod1 = name => _names.Add(name); | |
Action<string> actionMethod2 = delegate(string name) | |
{ | |
_names.Add(name); | |
}; | |
Action<string> actionMethod3 = AddName; | |
actionMethod1("bob"); | |
actionMethod2("jon"); | |
actionMethod3("fred"); | |
_names.ForEach(name => Console.WriteLine(name)); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment