473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Extension methods in static classes.

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

Oct 12 '08 #1
4 3290
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.

"Rene" <a@b.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
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
Oct 12 '08 #2
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.

"Family Tree Mike" <Fa************@ThisOldHouse.comwrote in message
news:u8****************@TK2MSFTNGP05.phx.gbl...
>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.

"Rene" <a@b.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
>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
Oct 12 '08 #3
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
Oct 12 '08 #4
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.

"Rene" <a@b.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
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
Oct 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
3
by: Deckarep | last post by:
I recently got into somewhat of a debate with a fellow co-worker. First I asked him if he new of a way to add a method to an existing class like enhancing a class but without using Inheritance. He...
8
by: Steven Nagy | last post by:
Some simple yes/no questions. I tried googling first and couldn't find specific answers. These are related to .Net 3.5 extension methods 1) I found that I would like to add properties as well as...
4
by: Steffen Bobek | last post by:
Extension methods are made for use with instances. I'd like to "misuse" them as static methods, too. Let me tell you my ambition: I use an extension method to serialize objects somehow like this:...
3
by: raylopez99 | last post by:
The headline says it all. Great minds think alike: read the blog below from three years ago, as endorsed by Ritchie, who coinvented C. BTW the below lambda expression code will not work (.Where...
3
by: johanatan | last post by:
When I first heard about these new features, I was very excited as it would have (if implemented as I had expected) rendered mimicking multiple inheritance almost painless in C#. Unfortunately,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.