Monday, January 14, 2019

Extension Methods in C#

Extension Method
         These are the special kind of methods those allows us to add methods to existing types without modifying the original types

How we can write the Extension Methods
          Few things we have to remember before we write a extension method
1. The wrapper class under which we declare the extension methods should be static class
2. Method should be static method

Lets get started

In this post I am going to add an extension method under string type.

Step 1 Write extension class
    public static class ExtensionStringMethods
    {
        public static string GetAllUpperCase(this string obj)
        {
            return (obj.ToString().ToUpper());
        }
    }
Step 2 Call extension methods
        private void GetUpper(string strInput)
        {
            strInput.GetAllUpperCase();
        }

No comments: