Extension method for static method?

  • Thread starter Harlan Messinger
  • Start date
H

Harlan Messinger

I have a feeling the answer is "No" but is there way to create an
extension method for a *static* method--say, if I wanted to extend the
File class by adding a static Length method to it?
 
J

Jeroen Mostert

I have a feeling the answer is "No" but is there way to create an
extension method for a *static* method--say, if I wanted to extend the
File class by adding a static Length method to it?

The answer is indeed no. And the reason is that while instance extension
methods allowed you to do something you couldn't do before (writing
MyCollection.Where(...).Select(...) instead of the increasingly cumbersome
Enumerable.Select(Enumerable.Where(MyCollection, ...), ...)), static
extension methods would only save you the hassle of coming up with a name
for your class if the good ones have been taken. The design team felt that
wasn't worth the potential confusion extension methods always introduce (I
would link to the blog post I read this in, but of course I can't find it now).

If you don't like the looks of MyFile or FileExtensions or FileSystem, I'd
just stick with the longer alternative "new FileInfo(f).Length". Or you can
always reference Microsoft.VisualBasic and use FileSystem.FileLen(). :)
 
A

Arne Vajhøj

I have a feeling the answer is "No" but is there way to create an
extension method for a *static* method--say, if I wanted to extend the
File class by adding a static Length method to it?

The question has been discussed before.

When it comes to features in programming languages then somebody
has to make some decisions.

Extension methods for instance methods ended up above the
line and extensions methods for class methods ended up
below the line.

Static extension methods is probably less useful than
instance extension methods.

And there are no obvious syntax for static extension methods.

Arne
 

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