It has been noted here (and quite rightly so) that extension methods in .NET are just syntactic sugar for static method calls on instance variables.
However in relation to this question, I am wondering why extension methods for static access were omitted from the language specification?
As well as being a .NET (C#) developer, I am also a JavaScript developer, and you can model the equivalent of static method extensions in JavaScript - Hence my reasoning for delving a little deeper into this argument. (I am well aware that .NET and JavaScript are two exceptionally different languages!)
Let's examine the syntax for an extension method and how it compiles:
public static string Hash(this string value)
{
// some logic here...
}
string x = "Hello World";
x.Hash();
compiles to
public static string Hash(string value)
{
// some logic here...
}
string x = "Hello World";
Hash(x);
If you try to create a variable for a static type
class MyClass
{
Console c; // error!
}
You can't!
If you try to pass a static type as a method parameter
public void DoThis(Console c) // error!
{
}
You can't!
So this defines the limitation of the language specification however in terms of an extension method, I can see the implementation being something along these lines...
public static void WriteLineInGreen(static Console c, string formatString, params object[] args)
{
c.ForeGround = ConsoleColor.Green;
c.WriteLine(formatString, args);
}
Console.WriteLineInGreen("Hello World {0}, {1}, {2}", ":-)", 1234, true);
Okay, it's not possible...why am I STILL asking? Answer..."Project Roslyn". Do you think this is something we might see along with all of the other language specification changes that will be implemented in Project Roslyn?
EDIT Anyone with any interest in Roslyn language extensions should watch this: https://channel9.msdn.com/Events/Build/2014/2-577