views:

99

answers:

4

Hello everybody

Static methods always should to encapsulate arguments by parenthesis . So it is much more easy to use extension methods while typing. That is one of the reason why i like extension methods.

I am not sure when time it is better to use extension or static methods. And i am thinking that what would be happen if all static methods would be default extension methods. Would be easy typing for extension methods but what other advantage or disadvantage of this idea ?

Edit

After i realize that not good idea to make all static methods to extension methods. For example : Methods that does not take argument or takes argument of different type. Also we can change the question. What would be happen if static methods would be usable by extension methods default for static methods which takes argument of its own type.

+2  A: 

Extension methods apply to instances of objects while static methods apply to types themselves. Static methods provide factory or utility functionality while extension methods add semantic value to specific objects.

For example, take Process.Start(ProcessStartInfo): it is a static method that gets called on the Process type. It would be weird for it to be an extension method since you'd have to create an instance of Process before actually calling it (note: yes there is a Process.Start method that operates on instances, but it is assumed that the Process object already has its start information populated).

If you want to extend the functionality of instances of a type and you can't change the original source, extension methods make sense. If you want to have static utility functions that are closely related to your type, static methods make sense. They each have their uses.

Chris Schmich
Charles makes a good point about all extension methods being static. Above, I am using "static" in the classic non-extension-method sense.
Chris Schmich
If an equivalent for Process.Start existed as an extension method, it would operate on an instance of the *ProcessStartInfo* class, not the Process class. (not endorsing the notion, but that would be the analog)
Kirk Woll
@Kirk: Where the method lives is not the point. He asked about what would happen if static methods became extension methods. My point is that it would become awkward since you'd be operating on instances (be they `ProcessStartInfo` or `Process`, it doesn't matter) instead of the types themselves. Extension methods and statics both have their uses, which is why there is support for both.
Chris Schmich
@Chris, that's all well and good. I was just nitpicking because the extension method *would* go on ProcessStartInfo and if you invoke it from there an argument could be made that it actually *is* intuitive. (still not the way I'd go though) :)
Kirk Woll
A: 

All extension methods are static methods.. The difference is that in an extension method, the first parameter in the method's signature must include the this keyword to indicate what type the method is an extension for... In a sense, the method is static to the type it is defined in, but, (as Chris mentions in his answer), they are non-static (instance) to the type defined in the first parameter...

If all static methods were extension by default, what would you do about old code that doesn't have the thgis keywpord? and how would the compiler know what type to 'attach' the extension method to?

Charles Bretana
Since the "this" modifier can only appear as the first parameter, the compiler doesn't face a very difficult choice. (though I still think it would be a bad idea)
Kirk Woll
+3  A: 

While many static methods, especially for value types and strings, operate on instances of themselves, there are static methods for which the extension syntax would not make sense because they do not act on their own type. ConfigurationManager, for example, is a "pure static" that has no instance component; it makes no sense to attempt to structure any such call as an extension method.

I have also gotten into situations where I had references to duplicate extension methods in different utility libraries with similar signatures; referring to the static class containing the method is the only way to resolve such ambiguities without a big refactor.

Lastly, extension methods are great in moderation. However, my current project has become perhaps a little too "fluent"; we have extension method wrappers for most of the String statics such as IsNullOrEmpty() and Format(), as well as parsing extension methods for every value type (wrappers for int.Parse, byte.Parse, DateTime.Parse, etc) and variations (such as TryParses, IsNullOrBlank, IsNotNullOrEmpty, etc.). There are a LOT of things you can do to a string instance, and in our project, most of them can be tacked on to the end of that instance (even a literal). That slows VS down considerably when you hit that period, and increases its memory footprint (and that of ReSharper, which provides IntelliSense extensions and using/reference suggestions).

KeithS
A: 

For one thing, there would be massive ambiguity when it comes to int.Parse(string), double.Parse(string), etc. all wanting to become extension methods on string at the same time. It's an important design decision as to whether the method should logically be extending the functionality of the operated-upon type, rather than being logically associated with the type defining the static method.

Dan Bryant