13 May 2016 Sudhanshu Ranjan 528

Extension Method In C# | MVC | Asp.net

 

An extension method has simplified calling syntax. It represents static methods as instance methods. An extension method uses the (this) keyword in its parameter list. It must be located in a static class.

Example: We are putting name as “IT@Pathsala#”

 

public static class SpecialWord
    {
        public static string RemoveWord(this string value)
        {

            if (value.Length > 0)
            {
                string[] Listofwords = { "?", "@", "#", "!", "^" };

                for (int i = 0; i < Listofwords.Length; i++)
                {
                    if (value.Contains(Listofwords[i]))
                    {
                        value = value.Replace(Listofwords[i], "-");
                    }
                }

                return value;
            }
            return value;
        }
    }

 

 

Output : IT-Pathsala

Summary:


1. An extension method will never be called if it has the same signature as a method defined in the type.
2. An extension method must be defined in a top-level static class.
3. An extension method with the same name and signature as an instance method will not be called.
4. Extension methods cannot be used to override existing methods.
5. The concept of extension methods cannot be applied to fields, properties or events.
6. Overuse of extension methods is not a good style of programming.

You can share at :

About Author
Sudhanshu Kumar Ranjan | Lead Engineer at HCL Technologies | Expertise in Microsoft Technology | Author | Blogger | Programmer
Know More
Connect with him on Facebook | Google+ | LinkedIn | Twitter

Recommended Post

Leave a comment

About Dotnet Cube

It's my blog - Dotnet Cube, where you will learn and get solutions about programming, web designing and development. I started this website to share my knowledge and experience which might help someone.

You can get more details about me and my blog at About Us page.

Subscribe Newsletter

Subscribe to our monthly newsletter to get the latest industry news, Media Group updates and our current specials.

Follow On Us...

Home | About | Advertise | Copyrights | Privacy | Contact | Admin

Copyrights © 2015-2017 dotnetcube.com. All rights reserved