What Are The Built-in Extension Methods In C#

A specific kind of static method called an extension method in C# enables you to expand the functionality of an existing type without changing its source code. The this keyword is used to describe the type that an extension method extends. Extension methods are classified as static methods in static classes. You may use an extension method by calling it just like you would a method of the type it extends.

  1. string.Join: Using a predetermined separator, this extension method concatenates an array’s items or a collection’s members.Here’s an example:
    string[] names = { "John", "Jane", "Jim" };
    string joinedNames = string.Join(", ", names);
    Console.WriteLine(joinedNames); // Output: John, Jane, Jim
  2. Array.Sort : This extension method uses ascending or descending order to arrange the elements in an array. The items are arranged by default in descending order.Here’s an example:
    int[] numbers = { 3, 1, 4, 2 };
    Array.Sort(numbers);
    foreach (int n in numbers)
    {
        Console.WriteLine(n);
    }
    // Output: 1, 2, 3, 4
    
  3. List.AddRange : A list of elements is added to a List<T> object via this extension method.Here’s an example:
    List<string> names = new List<string> { "John", "Jane" };
    names.AddRange(new string[] { "Jim", "Joan" });
    foreach (string name in names)
    {
        Console.WriteLine(name);
    }
    // Output: John, Jane, Jim, Joan
  4. Enumerable.Where : This extension method applies a conditional filter on a collection’s items.Here’s an example :
    int[] numbers = { 1, 2, 3, 4, 5 };
    IEnumerable<int> evenNumbers = numbers.Where(n => n % 2 == 0);
    foreach (int n in evenNumbers)
    {
        Console.WriteLine(n);
    }
    // Output: 2, 4
  5. Enumerable.Select : Each component of a collection is projected into a new form using this extension technique.Here’s an example :
    string[] names = { "John", "Jane", "Jim" };
    IEnumerable<int> nameLengths = names.Select(name => name.Length);
    foreach (int length in nameLengths)
    {
        Console.WriteLine(length);
    }
    // Output: 4, 4, 3
    
  6. Enumerable.FirstOrDefault : If the collection is empty, this extension method delivers a default value instead of the collection’s first member.Here’s an example :
    int[] numbers = { 1, 2, 3, 4, 5 };
    int firstEvenNumber = numbers.FirstOrDefault(n => n % 2 == 0);
    Console.WriteLine(firstEvenNumber); // Output: 2
    
  7. Enumerable.Aggregate : This extension method gives the components of a collection a specific accumulator function. Here’s an example :
    int[] numbers = { 1, 2, 3, 4, 5 };
    int sum = numbers.Aggregate((total, n) => total + n);
    Console.WriteLine(sum); // Output: 15
    

    The C# built-in extension methods are only a few examples. There are plenty additional alternatives, and you could even create your own special extension techniques to improve the use of existing types.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories