Showing posts with label Extension Method. Show all posts
Showing posts with label Extension Method. Show all posts

Sunday, 22 July 2018

Example of String Extension Method in .Net

In this post, I tried to explain the string extension method with an example. 

Most of the time, we are in the position to call the extension method by knowing or without knowing this as extension methods. In our coding life, we keep using the extension methods regularly. We know very well that the .NET Framework comes with a set of predefined classes, functions, and properties. In below example, I will explain how to create an extension method.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
​​

namespace ExtensionMethodDemo
{

    public static class StringExtension
    {
        public static String ChangeFirstLetterCase(this String stringToChange)
        {
            char[] charstr = stringToChange.ToCharArray();
            charstr[0] = char.IsUpper(charstr[0]) ? char.ToLower(charstr[0]) : char.ToUpper(charstr[0]);
            return new string(charstr);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string Str = "programming Trends";

            string Result = Str.ChangeFirstLetterCase();

            //string Result = StringExtension.ChangeFirstLetterCase(Str);

            Console.WriteLine(Result);

            Console.ReadKey();
        }

    }

}

What is Extension Methods in .Net?

​​
In this post, I will explain about What is Extension Methods in .net?.  An extension method is a method which used to extend a type, such as string or integer, without recompiling or modifying the original type. In essence, they are a type of static method, but they are called as if the method is native to the type. Extension methods are available from the 3.5 version of the .NET Framework and can be implemented on any type in the .NET Framework or any custom type that you define.

An extension method is a static method of a static class that can be invoked using the instance method syntax. Extension methods are used to add new behaviours to an existing type without altering. In the extension method, "this" keyword is used with the first parameter and the type of the first parameter will be the type that is extended by the extension method.

An extension method having the same name and signature like as an instance method will never be called since it has low priority than an instance method. The compiler doesn't cause an error if two extension methods with same name and signature are defined in two different namespaces and these namespaces are included in the same class file using directives. The compiler will cause an error if you will try to call one of them extension method. An extension method is only called when there is no native method found.
​​
Definition of extension method

Public Static class <class_name>
{
​​
        Public static <return_type>  <method_name>(this <type> <type_obj>)
        {
        }
}