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

Other Lessons for You

C#-Program Rectangle and Circle
using System;using System.Collections.Generic;using System.Linq;using System.Text; //Namespace declarationnamespace figures{ //Rectangle Class declaration class rectangle { // member variables...

Multi-core CPU utilization using Task Parallel Library in c#
Nowadays, we are using multiple core systems. We must write our .NET applications in such a way that we must utilise the complete computing power of the machine. The parallel task library(TPL) allows...

Trends in .Net Platform
.NET Framework is a software framework developed by Microsoft. It includes a large class library named Framework Class Library (FCL) and provides language interoperability (each language can use code written...

Coding Instructions To Prevent SQL-Injection
SQL-Injection is a very sensitive and very common technology to hack an application. I am describing a few techniques to make the SQL-Injection difficult. The main point of injection is a dynamic query....

Tips of learning Java Language/Other Programming Languages
1.You should know the basic concept: If we talk about programming languages so basic concept are same in all the high level languages. So you should know the basic concept firstly then you can easily understand...
I

ICreative Solution

0 0
0
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