UrbanPro
true

Learn C# .NET from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Extension Methods in C#

Reni Varghese
05/11/2016 0 0

Extension methods enables you to add methods to existing types without creating a new derived type, recompiling or otherwise modifying the original type. Extension methods are special type of static methods, but they are called as if they are instance methods on the extended type. For client code written in C# or Visual Basic there is no apparent difference between calling an extension method and the methods that are actually defined in the type. Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive. In your code you invoke the extension method with instance method syntax. However, the intermediate language (IL) generated by the compiler translates your code into a call on the static method. Therefore, the principle of encapsulation is not really being violated. In fact, extension methods cannot access private variables in the type they are extending.

Please find below an example of adding extension method to the string class.

namespace ExtenstionMethodDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "test String";
            string result = name.ChangeFirstLetterCase();
            Console.WriteLine(result);


        }
    }
}

 namespace ExtenstionMethodDemo
{
    public static class StringHelper
    {
        public static string ChangeFirstLetterCase(this string inputString)
        {
            if (inputString.Length > 0)
            {


                char[] charArray = inputString.ToCharArray();
                charArray[0] = Char.IsUpper(charArray[0]) ? Char.ToLower(charArray[0]) : Char.ToUpper(charArray[0]);

                return new string(charArray);
            }
            return inputString;
        }
    }
}

 

 

 

0 Dislike
Follow 0

Please Enter a comment

Submit

X

Looking for C# .NET Classes?

The best tutors for C# .NET Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C# .NET with the Best Tutors

The best Tutors for C# .NET Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more