Skip to content

Instantly share code, notes, and snippets.

@markcastle
Forked from AdamRamberg/ExtensionExample.cs
Created September 26, 2019 09:50
Show Gist options
  • Save markcastle/57f7b8c1f96628891e3a83086d39e3e3 to your computer and use it in GitHub Desktop.
Save markcastle/57f7b8c1f96628891e3a83086d39e3e3 to your computer and use it in GitHub Desktop.
Delegates and Extensions in Unity - ExtensionExample.cs
// Defines an add method that is chainable (returns itself)
public static List<T> ChainableAdd<T>(this List<T> list, T item)
{
list.Add(item);
return list;
}
// Usage
List<int> numbers = new List<int>();
numbers.ChainableAdd(1).ChainableAdd(2).ChainableAdd(3); // numbers will now contain [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment