0

Code below doesn't compile, because of 'Cannot convert expression type 'Response' to return type 'T'

    private T Test<T>() where T : SomeClass
    {
        return new SomeClass();
    }

The error seams reasonable when you try to return subclass of class specified in constaraint, but here the type is exactly the same. What is the reason of this behaviour?

vsevolod
  • 155
  • 2
  • 10
  • You can do the opposite: `private SomeClass Test() where T : SomeClass, new() { return new T(); }` Can you see why? – Jeppe Stig Nielsen Feb 18 '15 at 06:17
  • @JeppeStigNielsen you can do this, because you always return base type and any derived type can be implicitly converted to this type. For example you have DerivedClass and you call Test() which return DerivedClass instance. Since DerivedClass can by implicitly converted to SomeClass it is absolutely normal thing to do. – vsevolod Feb 18 '15 at 09:15
  • Exactly. Then you can also "see" the reason why the situation in your question is not allowed. Suppose you had `private T Test() where T : Animal { return new Animal(); }`. Then people could do `Giraffe g = Test();`. That would lead to a non-specific `Animal` instance being put into a variable `g` of type `Giraffe`. Then `g.EatLeaves()` would allow a "general" `Animal` instance to do something that not all animals can. – Jeppe Stig Nielsen Feb 18 '15 at 09:22
  • Thanks for such explanatory analogy. It seems absolutely logical now – vsevolod Feb 18 '15 at 09:32

2 Answers2

3

You cannot do it the way you describe it, because if you have a derived class, e.g.

public class SomeDerivedClass : SomeClass 
{
}

Your code is effectively trying to do the following which is obviously incorrect:

private SomeDerivedClass Test2()
{
    return new SomeClass();
}

It is possible to return a type T in method return statement, you only need to add a new() constraint to where clause

private T Test<T>() where T : SomeClass, new()
{
    return new T();
}
dotnetom
  • 24,551
  • 9
  • 51
  • 54
0

You can create an instance by adding the new() constraint and then creating an instance of T

private T Test<T>() where T : SomeClass, new()
{
    return new T();
}
Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102