Extension Methods are intended to be used as "if they were" regular instance methods of their target types:
var timeStamp = new DateTime().GetTimestamp();
other than that, there is no difference.
They were introduced in C# 3.0 to create the beautiful syntax we normally see in LINQ today, such as:
var soldProducts = customers.Where(x => x.IsActive)
.SelectMany(x => x.Orders)
.Where(x => x.Status == OrderStatus.Completed)
.Select(x => x.Product)
.ToList;
without extension methods, this syntax would not be possible and would require many regular method calls:
var activecustomers = Enumerable.Where(customers, x => x.IsActive);
var orders = Enumerable.SelectMany(activecustomers, x => x.Orders);
var completed = Enumerable.Where(orders, x => x.Status == OrderStatus.Completed);
var products = Enumerable.Select(completed, x => x.Product);
var soldProducts = Enumerable.ToList(products);
Not to mention that type inference
is also playing a big part here, since most of these methods in System.Linq.Enumerable
are actually generic methods with one or several type parameters, but the C# compiler is smart enough to infer the generic type parameters and remove the need to explicitly specify them.