public sealed class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
//Compile time error: 'Employee' cannot derive from sealed type 'Person'
class Employee : Person { }
our extension methods have to reside in a Static class and must reference the type they're extending using "this" keyword.
namespace pc.sealingAndExtending
{
public sealed class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
static class ExtensionMethod
{
//Notice the 'this' keyword in the argument for the extension method
public static string GetFullName(this Person p)
{
return $"{p.FirstName} {p.LastName}";
}
}
class Program
{
static void Main(string[] args)
{
var p = new Person { FirstName = "Pawel", LastName = "Ciucias" };
System.Console.WriteLine(p.GetFullName());
}
}
}