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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Jan 23, 2012, 12:29 AM
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.
Mukesh KumarPosted Jan 22, 2012, 8:45 AM
Check this link : http://msdn.microsoft.com/en-us/library/bb383977.aspx
VulpesPosted Jan 22, 2012, 8:37 AM
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.