Skip to content

Instantly share code, notes, and snippets.

@dlhartveld
Last active December 13, 2015 20:58
Show Gist options
  • Save dlhartveld/4974164 to your computer and use it in GitHub Desktop.
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.
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