質問

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

役に立ちましたか?

解決

You cannot create an instance of a static type. var c = new Console(); does not work. Therefore, you cannot have an argument with a static type. In Console c, what would c be? The syntax of your proposed static type extension method would have to be something like this:

public static void WriteLineInGreen(static Console, string formatString,
                                    params object[] args)
{
    Console.ForeGround = ConsoleColor.Green;
    Console.WriteLine(formatString, args);
}

I think that the language specification changes related to Roslyn are due to the fact that language specification inconsistencies and inaccuracies have been found while working on Roslyn. It seems not very plausible to me that any new C# language features are related to Roslyn.

Update: Well, I was wrong on this last point. As @svick points out in his comment, the much better structure of the new compiler makes it easier to implement language changes.

Another important reason is that the language and compiler development has become open source. Microsoft invites the community to participate. (See: C# 7 Work List of Features.)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top