Bug or by design

J

John B

Considering the below code, is it a bug that I can return
IEnumerable<AnyType> instead of only IEnumerable<IFu> as constrained in
the base class?

It is not possible to add a constraint to the concrete class
implementation of GetStuff as it errors saying that it inherits the
superclass constraint(s) (which makes sense).

interface IFu
{
int GetValue();
}

abstract class A
{
public abstract IEnumerable<T> GetStuff<T>() where T : IFu;
}

class B : A, IFu
{
public override IEnumerable<T> GetStuff<T>()
{
return (IEnumerable<T>)new List<object>(); //should this
generate a compile time error?
}

public int GetValue()
{
return 0;
}

}

class Program
{
static void Main(string[] args)
{
B b = new B();
foreach (IFu fu in b.GetStuff<B>())
{
Console.WriteLine(fu.GetValue());
}
}
}

TIA,
JB
 
J

John B

Paul said:
John B said:
Considering the below code, is it a bug that I can return
IEnumerable<AnyType> instead of only IEnumerable<IFu> as constrained
in the base class?
[...]
return (IEnumerable<T>)new List<object>(); //should this generate a
compile time error?

If you use the (Type)x notation, you can cast almost anything to
anything without the compiler noticing. However, it will fail at
run-time. Try something like this to convince yourself:

object s = "blah"; // evidently a string
int i = (int) s;

Casts are generally not type-safe, and you should only use them if
you're convinced they will succeed. Otherwise use the "as" keyword or
include some sort of conditional test.

Eq.
Yep, it just seems strange but when i think about it, its not really any
different to doing the below in a constrained generic method.

return (IEnumerable<IFu>)new List<object>();

Cheers,

JB
 
M

Marc Gravell

Can you give a bit more context here? There are ways of working with
inherited types and generics (using specific constraints), but whether
they apply depends on the scenario. There are also some LINQ
extensions (Cast, OfType) that might be of use, or you can implement
this type of cast directly using iterator blocks (yield return).

Of course - a better question would be why you need to use
List<object> in a generic type - that suggests you may have missed a
trick (sometimes necessary, but very rarely).

Marc
 
J

John B

Marc said:
Can you give a bit more context here? There are ways of working with
inherited types and generics (using specific constraints), but whether
they apply depends on the scenario. There are also some LINQ
extensions (Cast, OfType) that might be of use, or you can implement
this type of cast directly using iterator blocks (yield return).

Of course - a better question would be why you need to use
List<object> in a generic type - that suggests you may have missed a
trick (sometimes necessary, but very rarely).

Marc
Thanks Marc but thats ok.
I was just overriding an inherited method:
IEnumerable<T> GetSomething() where T : IMyInterface

When I overrode it, there were no constraints on the method and I just
thought it was strange.
But then I realized that the constraint enforcement is at the caller end
not at the implementation end.
So I can return anything but it will obviously result in a
invalidcastexception if it cannot be cast to the specified type.

In the end I did go with
foreach(IFu fu in fuItems)
{
yield return fu;
}


Thanks,
JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top