Tutorial by Examples

Extension methods were introduced in C# 3.0. Extension methods extend and add behavior to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. They are especially helpful when you cannot modify the source of a type you are looking to enhance. Ex...
Extension methods can also be used like ordinary static class methods. This way of calling an extension method is more verbose, but is necessary in some cases. static class StringExtensions { public static string Shorten(this string text, int length) { return text.Substring(0, ...
Extension methods are static methods which behave like instance methods. However, unlike what happens when calling an instance method on a null reference, when an extension method is called with a null reference, it does not throw a NullReferenceException. This can be quite useful in some scenarios....
public class SomeClass { public void DoStuff() { } protected void DoMagic() { } } public static class SomeClassExtensions { public static void DoStuffWrapper(this SomeClass someInstance) { someInstance.DoStuff(); // ok ...
Just like other methods, extension methods can use generics. For example: static class Extensions { public static bool HasMoreThanThreeElements<T>(this IEnumerable<T> enumerable) { return enumerable.Take(4).Count() > 3; } } and calling it would be like: ...
The static (compile-time) type is used rather than the dynamic (run-time type) to match parameters. public class Base { public virtual string GetName() { return "Base"; } } public class Derived : Base { public override string GetName() { ...
static class Program { static void Main() { dynamic dynamicObject = new ExpandoObject(); string awesomeString = "Awesome"; // Prints True Console.WriteLine(awesomeString.IsThisAwesome()); dynamicObject.StringValue = awesomeStrin...
Extension methods can be used for writing strongly typed wrappers for dictionary-like objects. For example a cache, HttpContext.Items at cetera... public static class CacheExtensions { public static void SetUserInfo(this Cache cache, UserInfo data) => cache["UserInfo"] ...
When an extension method returns a value that has the same type as its this argument, it can be used to "chain" one or more method calls with a compatible signature. This can be useful for sealed and/or primitive types, and allows the creation of so-called "fluent" APIs if the me...
It is very convenient to use extension methods with interfaces as implementation can be stored outside of class and all it takes to add some functionality to class is to decorate class with interface. public interface IInterface { string Do() } public static class ExtensionMethods{ pu...
You can use the following extension method for comparing the contents of two IList< T > instances of the same type. By default the items are compared based on their order within the list and the items themselves, passing false to the isOrdered parameter will compare only the items themselves ...
Extension methods are useful for adding functionality to enumerations. One common use is to implement a conversion method. public enum YesNo { Yes, No, } public static class EnumExtentions { public static bool ToBool(this YesNo yn) { return yn == YesNo.Yes; ...
Extension methods enable you to simplify your interface definitions by only including core required functionality in the interface itself and allowing you to define convenience methods and overloads as extension methods. Interfaces with fewer methods are easier to implement in new classes. Keeping o...
Extension methods can be used to "hide" processing of inelegant business rules that would otherwise require cluttering up a calling function with if/then statements. This is similar to and analogous to handling nulls with extension methods. For example, public static class CakeExtensions ...
Consider using Extension Methods as Functions which wrap other code, here's a great example that uses both a static method and and extension method to wrap the Try Catch construct. Make your code Bullet Proof... using System; using System.Diagnostics; namespace Samples { /// <summary&g...
One useful feature of extension methods is that you can create common methods for an interface. Normally an interface cannot have shared implementations, but with extension methods they can. public interface IVehicle { int MilesDriven { get; set; } } public static class Extensions { ...
We can create a better mapper classes with extension methods, Suppose if i have some DTO classes like public class UserDTO { public AddressDTO Address { get; set; } } public class AddressDTO { public string Name { get; set; } } and i need to map to corresponding v...
You can create extension methods to improve usability for nested collections like a Dictionary with a List<T> value. Consider the following extension methods: public static class DictListExtensions { public static void Add<TKey, TValue, TCollection>(this Dictionary<TKey, TColl...

Page 1 of 1