Extension methods in static classes.

R

Rene

Hi,

I was wondering if anyone could tell me why extension methods must be
declared on static classes. I mean, why not allowing them to be declared as
static methods in regular instance classes?

In case you are wondering, I am only curious to find out the reason for this
limitation. I don't really have a need to declare extension method on non
static classes. I am just curious.

Thanks
 
F

Family Tree Mike

I was surprised this was the case and tried it. The only thing I can think
of is that in a non-static class, "this" has a certain meaning that is
unnecessary in a static class. I suppose there could be confusion regarding
"this" in the method declaration within a non-static class at compile time.
 
R

Rene

If you take a look at the documentation for Extension Methods
(http://msdn.microsoft.com/en-us/library/bb383977.aspx), there is a
statement there that says:

"In fact, extension methods cannot access private variables in the type they
are extending"

So could this be perhaps one of the reasons why extension methods are not
allowed in instance classes? I am thinking that if such thing was allowed,
you could do something like the snippet below:

--------------------------

class Foobar
{
private int privateVar;

public static void DoIt(this Foobar f)
{
f.privateVar = 123;
}
}

--------------------------

In this snippet, you can see that the extension methods *would* have access
to private methods and this would violate what is stated on the
documentation.

Of course, I realize that doing something similar to what I did on my
example is pretty pointless and stupid but that is beside the point. All I
really wanted to demonstrate is how the rule of extension methods not having
access to private member can be violated if extension methods where allowed
to be declared in instance classes.

Any thoughts?

Thanks.
 
M

Marc Gravell

Not a technical point, and I don't claim it to be an actual reason;
more of an aside: single responsibility... if the class representing a
Person (etc), or is it providing extension methods to something else?
It probably shouldn't be adding extension methods to itself, since
that could be better-done with an instance method... the only
(questionable) benefit would be the ability to call the method on a
null instance...

Marc
 
W

William Stacey

I think it may also be to help avoid confusion. Keep one static class with
your extention methods for a namespace - kinda like a declaration section.
Could you imagine the mess one could create if you sprinkled extentions
methods all over the place in many classes just because at the time it was
easier to "insert method here". I think I like that they must be a static
class from a convension point of view.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top