A Responsive Blogger Website, That Take my blog to the next level.

I am a Software Developer, Learning and experementing with .Net Technology and try to put my Learning altogether here.


We have all sections related to .Net Technology, like .Net-C#, Asp.Net, MVC, jQuery, AngularJs, Sharepoint. Find out more...


Following are the some of the Advantages of using this Web site :-

  • Get Update about latest in .Net world.
  • Collection of Useful, Frequently used .Net Solutions.
  • It’s beautiful on every screen size (try resizing your browser!)
by

.Net Interview Question : Hidden Fact About Extension Method


What are Extension Methods?
According to MSDN, Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.
Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. 

How to implement extension methods?
We want to define a method in the string class (let's call it ChangeFirstLetterCase), which will change the case of the first letter of the string. For example, if the first letter of the string is lowercase the function should change it to uppercase and viceversa.
We want to be able to call this function on the string object as shown below.
string result = strName.ChangeFirstLetterCase();

Defining ChangeFirstLetterCase() method directly in the string class is not possible as we don't own the string class. It belongs to .NET framework. Another alternative is to write a wrapper class as shown below.
public class StringHelper
{
    public static string ChangeFirstLetterCase(stringinputString)
    {
        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;
    }
}

Wrapper class works, but the problem is, we cannot call ChangeFirstLetterCase() method using the following syntax.
string result = strName.ChangeFirstLetterCase();

Instead we have to call it as shown below.
string result = StringHelper.ChangeFirstLetterCase(strName);

Convert ChangeFirstLetterCase() method to an extension method to be able to call it using the following syntax, as though it belongs to string class.
string result = strName.ChangeFirstLetterCase();

To convert ChangeFirstLetterCase() method to an extension method, make the following two changes.

1. Make StringHelper static class
2. The type the method extends should be passed as a first parameter with this keyword preceeding it.

With these two changes, we should be able to call this extension method in the same way we call an instance method. Notice that the extension method shows up in the intellisense as well, but with a different visual clue.
string result = strName.ChangeFirstLetterCase();

Note: we should still be able to call this extension method using wrapper class style syntax. In fact, behind the scene this is how the method actually gets called. Extension methods are just a syntactic sugar.
string result = StringHelper.ChangeFirstLetterCase(strName);

0 comments :