I'm not entirely sure as to how to figure out if a method is an extension method or not. I read the following guidelines:
The method has to be in a static class (for example "IntExtensions").
The method needs to be "public static" (for example
public static int Half(this int source)
)
My issues with the extension methods are:
- I don't understand is how we know which class the method is extending. Is it the class that has
this
preceding it? So in the example above it would be string? - What role does the static class that encloses the method play? The whole syntax seems very confusing.
- couldn't I group a number of different extension methods for different classes under the same class ( which doesn't seem to make sense since I want to extend only 1 class ) For example:
/
public static class IntExtensions
{
// method has to be static, first parameter is tagged with the 'this'
// keyword, the type of the parameter is the type that is extended.
public static int Half(this int source)
{
return source / 2;
}
public static string foo( this string s)
{
//some code
}
}