By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,694 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,694 IT Pros & Developers. It's quick & easy.

Array casting bug??

P: n/a
If variance/covariance is not allowed in array of value types, why is the
expression on the "Main" method run successfully (see snippet below)? I am
missing something?

Thank you.

---------------------
class Program
{
enum Foo
{
a, b, c
}

enum Bar
{
x, y, z
}

static void Main(string[] args)
{
Bar[] barArray = (Bar[])System.Enum.GetValues(typeof(Foo));
}
}
---------------------

Oct 10 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
If you want an array of the Enum type, you have to loop. If you want an
array of strings, you can use GetValues or GetNames, depending on what you
are aiming at.

I can't think of any other way off hand.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Rene" <a@b.comwrote in message
news:uT**************@TK2MSFTNGP06.phx.gbl...
If variance/covariance is not allowed in array of value types, why is the
expression on the "Main" method run successfully (see snippet below)? I am
missing something?

Thank you.

---------------------
class Program
{
enum Foo
{
a, b, c
}

enum Bar
{
x, y, z
}

static void Main(string[] args)
{
Bar[] barArray = (Bar[])System.Enum.GetValues(typeof(Foo));
}
}
---------------------
Oct 10 '08 #2

P: n/a
On Thu, 09 Oct 2008 20:16:57 -0700, Rene <a@b.comwrote:
If variance/covariance is not allowed in array of value types, why is
the expression on the "Main" method run successfully (see snippet
below)? I am missing something?
I think so. But it's not necessarily something obvious. :)

A question nearly the same as yours came up a few months ago. You might
find that thread relevant in addressing your question:
http://groups.google.com/group/micro...1bf036a23918e/

The big difference between that thread and your question is that in the
previous thread, the person was using the "as" operator, whereas you're
using a cast. But I think in this particular example, the two are
basically equivalent (except for what happens on an error). That is, the
statement is legal C# and the error-checking is done by the CLR, which
allows it because the underlying enum types are the same.

In other words, I think that this particular array cast is in fact legal.
It's not a bug for it to work.

Pete
Oct 10 '08 #3

P: n/a
Hi Greg,

What I wanted to know is why the code is not throwing an error. Casting
from Foo[] to Bar[] is illegal so Enum.GetValues() should throw an error
when it tries to cast the return value Foo[] (Array) to Bar[].

At least that's why I think, I am sure I am probably wrong about my
assumption. That is why I would like to know if someone could clarify.

Thanks.

"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamMwrote in
message news:%2****************@TK2MSFTNGP05.phx.gbl...
If you want an array of the Enum type, you have to loop. If you want an
array of strings, you can use GetValues or GetNames, depending on what you
are aiming at.

I can't think of any other way off hand.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Rene" <a@b.comwrote in message
news:uT**************@TK2MSFTNGP06.phx.gbl...
>If variance/covariance is not allowed in array of value types, why is the
expression on the "Main" method run successfully (see snippet below)? I
am missing something?

Thank you.

---------------------
class Program
{
enum Foo
{
a, b, c
}

enum Bar
{
x, y, z
}

static void Main(string[] args)
{
Bar[] barArray = (Bar[])System.Enum.GetValues(typeof(Foo));
}
}
---------------------
Oct 10 '08 #4

P: n/a
Rene <a@b.comwrote:
What I wanted to know is why the code is not throwing an error. Casting
from Foo[] to Bar[] is illegal so Enum.GetValues() should throw an error
when it tries to cast the return value Foo[] (Array) to Bar[].

At least that's why I think, I am sure I am probably wrong about my
assumption. That is why I would like to know if someone could clarify.
A very similar question has come up before, and I asked Eric Lippert
(on the C# team) about it. Basically C# "trusts" the CLR to permit/deny
appropriate conversions, and on this front the CLR is slightly more
permissive than we'd expect.

Here's the previous thread:

http://groups.google.com/group/micro...nguages.csharp
/browse_thread/thread/2d21bf036a23918e/5a5c351206ebd999

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 10 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.