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 #2:
I have learned the folly of my ways by trying to use an IParsable, as suggested by many below. The example I made follows. Of course, there is no way to resolve the call to TryParse…
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;
}
No, the CLR doesn’t have such a thing as static interfaces. I’ve surmised that they’d be useful for generic type constraints, but that’s the only use I can see for them… otherwise how would you use
IParsablein your example? How would you pass around the type that was able to parse it?See this blog post for more details of what I was envisaging.