Does C# contain IParsable<T> or ITryParsable<T>?

c#

Solution

Since C# does not support static interfaces, you would have to have an instance of the object in order to call the parse method. You would end up with something like this:

var a = new int().Parse<int>("123");
var b = 123.Parse("567");

Or with the `TryParse` method things get even more weird:

int x;
if (x.TryParse("456", out x))
    // trippy... now imagine that x is a reference type...

Problem

Obviously it would be fairly simple to implement the following interfaces for your own solution ``` public interface IParsable<T> { T Parse(string s); } public interface ITryParsable<T> : IParsable<T> { bool TryParse(string s, out T output); } ``` Having been writing various ways of parsing unknown typed user input data, I would have found having `int`, `decimal`, etc, etc implement a version of these interfaces indispensable. To me it seems like a fairly obvious thing to have included in the `System` namespace. Obviously this is not the case. So what is the best way of seeing whether a class "implements" these interfaces? Checking whether the method exists via Duck Typing seems like a sensible alternative, but Reflection isn't terribly performant. It looks like this is not possible as C# does not allow static methods in interfaces.

Original source

Related problems