471,336 Members | 1,294 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

Bug or by design

Considering the below code, is it a bug that I can return
IEnumerable<AnyTypeinstead of only IEnumerable<IFuas 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<TGetStuff<T>() where T : IFu;
}

class B : A, IFu
{
public override IEnumerable<TGetStuff<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
Jun 27 '08 #1
4 927
"John B" <jb******@yahoo.comwrote:
Considering the below code, is it a bug that I can return
IEnumerable<AnyTypeinstead of only IEnumerable<IFuas 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.
Jun 27 '08 #2
Paul E Collins wrote:
"John B" <jb******@yahoo.comwrote:
>Considering the below code, is it a bug that I can return
IEnumerable<AnyTypeinstead of only IEnumerable<IFuas 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
Jun 27 '08 #3
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<objectin a generic type - that suggests you may have missed a
trick (sometimes necessary, but very rarely).

Marc
Jun 27 '08 #4
Marc Gravell wrote:
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<objectin 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<TGetSomething() 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
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by zlst | last post: by
reply views Thread by Edward Diener | last post: by
2 posts views Thread by Paul Cheetham | last post: by
1 post views Thread by Nogusta123 | last post: by
reply views Thread by YellowFin Announcements | last post: by
19 posts views Thread by neelsmail | last post: by
10 posts views Thread by vital | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.