Last active
December 13, 2015 20:58
-
-
Save dlhartveld/4974164 to your computer and use it in GitHub Desktop.
Demonstration of extension methods.
Note the static class PersonExtensions, the static method NameLenght, and the this keyword in front of the Person parameter.
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
class Person | |
{ | |
private string name; | |
public Person(string name) | |
{ | |
this.name = name; | |
} | |
public string Name { get { return this.name; } } | |
} | |
static class PersonExtensions | |
{ | |
public static int NameLength(this Person person) | |
{ | |
return person.Name.Length; | |
} | |
} | |
class Work | |
{ | |
static void Main(string[] args) | |
{ | |
Person p = new Person("John"); | |
Console.WriteLine("Name length: {0}", p.NameLength()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment