10

Consider the following generic method:

public T2 Frob<T1, T2>(T1 item)
        where T1 : class, T2
        => item as T2;

The compiler will refuse to compile this code; The type parameter 'T2' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint

Ok, this is easily solvable simply doing:

public T2 Frob<T1, T2>(T1 item)
        where T1 : class, T2
        where T2 : class
        => item as T2;

But isn't this redundant? Is there any possible T2 that is not a class considering the constraints already in place for T1?

My question is not why this "inference" wasn't implemented in the compiler, the reason could simply be "no one thought about it" and thats OK. I'm more interested in knowing if my reasoning is correct in that T2 is effectively and in all cases constrained to class in the first example even if its not explicitly enforced.

13
  • 1
    @HimBromBeere give me one example of a reference class deriving from a value type, because that is precisely what must happen with the constraints in place for T1. Commented Feb 13, 2017 at 16:14
  • 1
    @HimBromBeere I think what OP means is that T2 cannot be a value type because T1 cannot be derived from a value type. So it necessarily has to be a reference type. Commented Feb 13, 2017 at 16:15
  • 4
    Personally I find these questions "Primarily opinion based" but questions about "Why did the C# designers implement X in the compiler" have been answered before so... what do I know. Commented Feb 13, 2017 at 16:18
  • 1
    @Jamiec If you read my question, I'm not asking why the compiler needs a redundant constraint. I'm asking if it is redundant or if there is some very strange corner case where it wouldn't be so. I can't think of any but I'd like to be 100% sure. Commented Feb 13, 2017 at 16:25
  • 3
    One way or another, you're asking why a human being implemented it that way, or whether said human made an oversight. That's a question of motivation, and the only person who could answer it is the human who implemented it. Commented Feb 13, 2017 at 16:28

1 Answer 1

4

My interpretation of this, given the C# 5.0 specs say at 7.10.11, The as operator:

In an operation of the form E as T, E must be an expression and T must be a reference type, a type parameter known to be a reference type, or a nullable type.

The compiler at that point only considers T2 in this block:

public T2 Frob<T1, T2>(T1 item)
        where T1 : class, T2
        => item as T2;

And it sees that T2 itself is not constrained. Sure, it could deduct that in this case T1 is expected to be a reference type and inherit T2, therefore that T2 itself should also be a reference type, but I'm sure there are reasons for not doing that.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually there probably aren't reasons for doing it other than what Eric Lippert notes in this answer: stackoverflow.com/a/1843557. TL;DR: "Features take time to implement."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.