472,972 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,972 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 974
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: adb | last post by:
I came up with a replication configuration that is basically the result of all the restrictions of replication as well as the restrictions of allowable software on work PC's and I was curious if...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
0
by: Edward Diener | last post by:
In Borland's VCL it was possible to divide a component into design time and run time DLLs. The design time DLL would only be necessary when the programmer was setting a component's properties or...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
2
by: Paul Cheetham | last post by:
Hi, I have moved an application from VS2003 to VS2005, and I am now unable to view most of my forms in the designer. The majority of the forms in my project are derived from class PACForm,...
1
by: Nogusta123 | last post by:
Hi, I have had a lot of problems getting web pages, master pages and content pages to render in VS2005 design view the same as they would in Internet Explorer. I did a lot of looking on the...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
10
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
4
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.