Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How to write common method and how to write extented method in c#?
please post with sample common method?
Posted
Updated 1-Feb-11 1:18am
v2
Comments
Estys 1-Feb-11 7:17am    
Do you mean extension method?
Umair Feroze 1-Feb-11 7:26am    
Please be specific and provide the scenario for it

There is a sample for extension methods here[^].

Im not quite sure what you mean by common methods.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Feb-11 13:16pm    
Well, right place to read, a 5.
--SA
Abhinav S 1-Feb-11 23:17pm    
Thanks SA.
I have no idea what you mean by a "common" method.

An extension method is a method that extends a class without having to derive a whole new class to implement new functionality.

You need to create public static class (it can be given any name you want because it's static, and you don't have to instantiate it), and put your method in it:

C#
public static class ExtensionMethods
{
    public static string Append(this string text, string newText)
    {
        string result = text + newText;
        return result;
    }
}


You call it like this:

C#
string x = "123";
x = x.Append("456");


After the call to Append, the variable x will contain "123456".

Notice that the method MUST be static, and then notice the parameters. the first one indicates the object being extended, and the 2nd one is the text to be appended.

You really should learn how to use google, because hat's how you're going to learn the details about extension methods.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 1-Feb-11 13:15pm    
My 5. Good note about "common" gibberish.
I would add strong loud RTFM though, but you know better.
--SA
I believe the OP's confused about terminology here. I suspect by common methods he means static methods. And by extended methods he means methods that override base methods. Apparently he has a Java background.

For info on static classes/methods, see:

http://msdn.microsoft.com/en-us/library/79b3xss3(v=VS.100).aspx[^]

For info on inheriting and overriding methods, see:

http://msdn.microsoft.com/en-us/library/ms173149.aspx[^]

http://msdn.microsoft.com/en-us/library/6fawty39.aspx[^]
 
Share this answer
 
Comments
Espen Harlinn 14-Feb-11 10:20am    
Good links, should be helpful :)
Nish Nishant 14-Feb-11 10:24am    
Thank you. Obviously everyone's guessing at what the OP meant here :-)
Espen Harlinn 14-Feb-11 14:19pm    
I thought it a good guess :)
Hope C-sharp-30-Extension-Methods/[^]will give you an idea.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900