Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.