Hi..
I am working in c# and use Extension Method. Define Extension Method and syntax?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jan 22, 2012, 3:37 AM EST
For example:
static class StringExtensions
{
public static string Reverse(this string s)
{
// code
}
}
Such a method can be used in C# as though it were an instance method of whatever type is qualified by 'this' in this case string. So you can write:
string s = "Hello";
string r = s.Reverse();
For this to work, the static class needs to be in scope - typically you ensure this by placing the static class in a namespace and then 'using' the namespace.
If the type in question already has a method with the same name as the extension method, then the former takes precedence.
For further details and examples, please see our recommended articles in the sidebar to the right of this thread.
Mukesh KumarRank #615 · 3.9K reputationPosted Jan 22, 2012, 3:45 AM EST
Check this link : http://msdn.microsoft.com/en-us/library/bb383977.aspx
Jignesh TrivediRank #16 · 50.3K reputationPosted Jan 22, 2012, 7:29 PM EST
try,
static void Main(string[] args)
{
var s = "".ToStringExtension();
}
public static class Extensions
{
public static string ToStringExtension(this string s)
{
return Convert.ToString(s);
}
}
http://weblogs.asp.net/andrewrea/archive/2008/05/25/extension-methods-inside-net-3-5.aspx
hope this help.