473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Array casting bug??

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
4 1426
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal...
18
by: Kyro | last post by:
New to C# (migrating from Delphi) and I'm not sure how to get a delimited string into an array of string. input: string looks like - 01-85-78-15-Q11 output: an array with elements - ...
15
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.