Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Created February 14, 2013 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yetanotherchris/4956191 to your computer and use it in GitHub Desktop.
Save yetanotherchris/4956191 to your computer and use it in GitHub Desktop.
Action<T> examples in C#
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