Is the CLR capable of static interfaces?

And if so, why can't you do this:

public interface IParsable
{
    static IParsable Parse(string s);
    static bool TryParse(string s, out IParsable);
}

in c #?

EDIT: Or, alternatively:

public interface IParseable<T>
{
    static T Parse(string s);
    static bool TryParse(string s, out T);
}

EDIT No. 2: I found out the stupidity of my paths by trying to use IParsable, as suggested by many below. The following example. Of course, there is no way to allow the TryParse call ...

public IParsable ReadFromKeyboard()
{
    IParsable ToReturn;
    bool FirstTry = false;
    bool Success;
    do
    {
        if (!FirstTry)
            DisplayError();
        AskForInput();

        Success = IParsable.TryParse(Console.ReadLine, out ToReturn);
        FirstTry = false;
    } while(!Success)

    return ToReturn;
}
+3
source share
6 answers

No, the CLR does not have such a thing as static interfaces. I suggested that they would be useful for generic type constraints, but this is the only thing I see for them ... otherwise, how would you use it IParsablein your example? How would you skip a type that could parse it?

. , .

+5

, .

+3

CLR . .

- , , CLR (, ).

, ( ) : . .

+3

, . :

. , . .

+2

# , - . , virtual ( ) .

, . , , .

, , , , . .

+2

, CLR . , .

Other similar functions that .NET (well, C #) do not have: Haskell class classes , F # element restrictions for generic types

0
source

Source: https://habr.com/ru/post/1756242/


All Articles