Created
August 3, 2012 11:36
-
-
Save lurongkai/3246835 to your computer and use it in GitHub Desktop.
C# extension method
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 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