Array casting bug??

R

Rene

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));
}
}
---------------------
 
C

Cowboy \(Gregory A. Beamer\)

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! |
********************************************
 
R

Rene

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) said:
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 said:
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));
}
}
 
J

Jon Skeet [C# MVP]

Rene said:
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/microsoft.public.dotnet.languages.csharp
/browse_thread/thread/2d21bf036a23918e/5a5c351206ebd999
 

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