![]() |
Languages »
C# »
General
Beginner
License: The Code Project Open License (CPOL)
Extension Method (Basic Understanding)By rajeshrocksExtension method is a feature in C# 3.0, which allows a developer to add functionality in existing class without modifying or recompiling or extending the existing class. |
C# (C# 3.0, C# 4.0), .NET (.NET 3.0, .NET 3.5, .NET 4.0), Visual Studio (VS2008), Architect
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This is a new feature in C# 3.0 that helps to extend existing classes like .NET core classes which cannot be extended. You may need to add functionality without extending the class, or recompiling the existing class or modifying the existing method.
static method but called with the syntax of an instance method. Extension method, then member method will get precedence over Extension method. For example: Show method is a member method of Message class. So if there is any extension method called Show on type Message class that is created, always Show member method will get precedence over Show extension method for the type Message.
Compiler signature of Extension method is as follows:
static class Extensions
{
public static IEnumerable<T> Where<T>(this IEnumerable<T> sequence,
Predicate<T> predicate)
{
foreach (T item in sequence)
{
if (predicate(item))
{
yield return item;
}
}
}
}
static. this”. ref, out, etc. are allowed with “this” modifer or instance parameter. public. public static int Obj<T> (this T param)
public members of the target type. Extension method conflicts with a member method of target type, always member method gets invoked instead of Extension method. static visible class to contain Extension method. Extension method as static method. this” modifer. Extension method with using directive. In the first example, I will add an Extension method to the existing String class. This Extension method will remove all the vowels from the string.
Modify the class with modifiers public and static of the class extensionmethodcontainer.
Add an Extension method with the below signature. The first parameter String specifies that this is an Extension method on the type String.
public static String RemoveVowel(this String s)
The full code to remove vowel from input string is written in the Extension method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExtensionMethodSample
{
public static class extensionmethodcontainer
{
public static String RemoveVowel(this String s)
{
string[] vowels = new string[] { "A", "E", "I", "O", "U" };
if (string.IsNullOrEmpty(s))
return string.Empty;
List<char> chars = new List<char>(s.ToCharArray());
for (int i = chars.Count - 1; i >= 0; i--)
{
for (int j = 0; j < vowels.Length; j++)
{
if (chars[i].ToString().ToLower() == vowels[j].ToLower())
chars.RemoveAt(i);
}
}
return new string(chars.ToArray());
}
}
}
Client code is in the Main class.
In the main class, user inputs the string and RemoveVowel Extension method is called on the input string to remove vowel from the string.
Note
Extension method and client are in the same namespace, so there is no need to include namespace of the Extension method. Extension method is called as any other member method.
resultString = str.RemoveVowel();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExtensionMethodSample
{
class Program
{
static void Main(string[] args)
{
String resultString;
Console.WriteLine("Enter Input String to Remove all Vowel using " +
"Extension Method \n");
String str = Console.ReadLine();
Console.WriteLine("After Removing Vowel Input String is \n");
resultString = str.RemoveVowel();
Console.WriteLine(resultString);
Console.ReadKey();
}
}
}
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 12 Jun 2009 Editor: Deeksha Shenoy |
Copyright 2009 by rajeshrocks Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |