Skip to main content

C# Extension Methods

24 Jan 2020 - Cody Merritt Anhorn

Use Extension methods to add functionality to already compiled/existing objects, doing so allows for simplified abstractions to be created for complex objects. This also allows for the ability to extend primitive types or framework provided objects.

Below is an extension method that does a null check, including a bonus attribute to help with fixing the false negatives in Visual Studio for CA1062.

using System;

public static class ObjectExtensions
    public static void NullCheck(
        [ValidatedNotNull] this object objectToCheck,
        string paramName = ""
        if (objectToCheck == null)
            throw new System.ArgumentNullException(
internal sealed class ValidatedNotNullAttribute : Attribute

Categories: blog dotnet

Tags: .NET C#

Cody Merritt Anhorn