Dudeman3000
Dudeman3000

Reputation: 601

.Net Core - does something like IConvertible<T> exist?

I want to define an interface that specifies a method signature that returns a concrete type (with no parameters.) So something like:

interface MyInterface
{
    ConcreteType GetConcreteType();
}

So that's fine, I can do that, but if I wanted to specify a compare-to-concrete-type method, I could define my interface as inheriting from the IComparable interface. Something like this:

interface MyInterface : IComparable<MyConcreteClass>
{
    // no method signature required bc signature defined in IComparable<T>
}

Is there a native SDK construct for this - an interface that defines a Convert method?

I expected to find an IConvertible<T> interface but it looks like IConvertible is about something else (At a glance, I think it defines signatures that will make your class work as a parameter to the Convert class like, something like this: int myInt = Convert.ToInt32(MyClassThatImplementsIConvertible);

I just wrote this interface that does what I want but I'm thinking something like this already exists, yes? no?

public interface IConvertibleTo<T>
{
    void ConvertTo(out T convertedObj);
}

I used an out parameter bc I couldn't figure out how a class would implement multiple types of this interface, so: public MyClass: IConvertibleTo<ClassA>, IConvertibleTo<ClassB> { } without getting methods with the same signatures but different return types.

Upvotes: 0

Views: 105

Answers (0)

Related Questions