PC Review


Reply
Thread Tools Rate Thread

Array casting bug??

 
 
Rene
Guest
Posts: n/a
 
      10th Oct 2008
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));
}
}
---------------------

 
Reply With Quote
 
 
 
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      10th Oct 2008
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" <(E-Mail Removed)> wrote in message
news:uTW$(E-Mail Removed)...
> 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));
> }
> }
> ---------------------
>


 
Reply With Quote
 
Rene
Guest
Posts: n/a
 
      10th Oct 2008
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)" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:uTW$(E-Mail Removed)...
>> 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));
>> }
>> }
>> ---------------------
>>

>


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10th Oct 2008
Rene <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting an Array of Objects to a typed Array Phil Jones Microsoft VB .NET 7 15th Nov 2004 10:17 PM
Casting an Array of Objects to a typed Array Phil Jones Microsoft Dot NET Framework 2 14th Nov 2004 09:31 AM
Re: Casting an array Jay B. Harlow [MVP - Outlook] Microsoft Dot NET 0 13th Jul 2004 04:55 PM
Casting managed array Stewart Stevens Microsoft Dot NET 4 8th Jul 2004 11:52 PM
casting a bit array to a structure Joe Thompson Microsoft VC .NET 6 19th Dec 2003 02:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 PM.