For the following generic c# class, I'd like to convert T to K:
public abstract class ValueType<T,K> : IValueType<T> where K : ValueType<T,K>,new()
{
public abstract T Value { get; set; }
public static implicit operator ValueType<T,K>(T val)
{
K k = new K();
k.Value = val;
return k;
}
}
If I were to implement a direct operator implicit operator K(T val)
it would result in a compile-time error (CS0556).
I thought I could try chaining implicit operators:
public static implicit operator K(ValueType<T,K> vt){
return (K)val;
}
but the following example still complains that it can't be converted:
public class Test : ValueType<int, Test>
{
public override int Value{ get; set; }
}
Test t = 6; //complains it's unable to be converted
Console.WriteLine(t.Value);
I really want to avoid explicitly casting if possible.
This question extends upon another SO question I've previously raised.
The rules for implementing your own implicit conversion logic are quite strict and you should probably become very familiar with sections 6.4.4 and 10.10.3 of the specification if you're going to do particularly complicated ones like this.
Briefly, a few key rules you should know are:
Casts are not chained by the compiler, so that way of solving the issue doesn't work.
Implicit casts are quite strict in the type checking. Your very first snippet and the Test
class do work if the compiler knows the type:
ValueType<int, Test> t = 6;
Console.WriteLine(t.Value);
The problem is that your ValueType<int, Test>
- from the type system point of view - is not always a Test
, so that the implicit conversion doesn't apply there.
Eric Lippert wrote a blog post on this kind of generic self-referencing by the way - worth a read!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With