Skip to content

Instantly share code, notes, and snippets.

@lurongkai
Created August 3, 2012 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lurongkai/3246835 to your computer and use it in GitHub Desktop.
Save lurongkai/3246835 to your computer and use it in GitHub Desktop.
C# extension method
public class TestClass
{
public int NormalProperty { get; private set; }
public void NormalMethod() {
Console.WriteLine("Normal Method.");
}
}
public static class TestExtension
{
public static void ExtensionMethodForTestClass(this TestClass testClass, string arg) {
var format = "property value:{0}, arg value:{1}";
Console.WriteLine(String.Format(format, testCLass.NormalProperty, arg);
}
}
public class Program
{
public void Main(string[] args) {
var testClass = new TestClass();
// this style
testClass.ExtensionMethodForTestClass("test arg");
// will compile to
TestExtension.ExtensionMethodForTestClass(testClass, "test arg");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment