C# Enums - Feature or deficiency

  • Thread starter Thread starter Ian Semmel
  • Start date Start date
I

Ian Semmel

If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];
 
Well, seeing as how what you want to do is so obvious, maybe you'd like to
explain what you want to do so that we, who are obviously deficient in the
area of ESP, can get an idea.
 
The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

Sadly, no. At least not in the general case. (Although you can use
Length instead of GetLength(0) which is a little bit shorter.)

In your particular case you could simply use (int) en.i3 + 1 because
the default base type of an enumeration is int (Int32) and values
start with zero, incrementing by one with each extra value.

But that requires changing your code whenever you add an enum value.
You could add an extra value, such as "last = i3", and use that
instead for array dimensioning etc. But you'd still have to remember
to update this "last" value when the enumeration changes.
 
Ian Semmel said:
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

I would do this (but I'm not a purest):

Enum en
{
i1,
i2,
i3,

enumLength
}

int[] v = new int[enumLength];
 
Ian said:
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

..NET arrays are indexed arrays, not associative arrays. The only way you
can index an array in .NET is through an integer, which is why the
compiler doesn't like you trying to use an enum as its index.
 
Ian said:
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

C# is of the C++/Java family - you only specify number
of elements in the array N and array indexes are integers
0..N-1 !

Using types as an array index is a Pascal/Ada family
thing.

Arne
 
-----Original Message-----
From: BobF [mailto:[email protected]]
Posted At: Sunday, 9 December 2007 10:31 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: C# Enums - Feature or deficiency
Subject: Re: C# Enums - Feature or deficiency


Ian Semmel said:
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

I would do this (but I'm not a purest):

Enum en
{
i1,
i2,
i3,

enumLength
}

int[] v = new int[enumLength];

Yes, that is the technique I use in C/C++. The reason is that it makes
programs self-resizing as if I add an i4, I don't have to change
anything else.

From other replies in the thread, it seems that C# doesn't allow enums
to be used as array indexes which to me seems like a hole in the
specification as the two are linked in my opinion.
 
-----Original Message-----
From: Arne Vajhøj [mailto:[email protected]]
Posted At: Monday, 10 December 2007 2:21 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: C# Enums - Feature or deficiency
Subject: Re: C# Enums - Feature or deficiency

Ian said:
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

C# is of the C++/Java family - you only specify number
of elements in the array N and array indexes are integers
0..N-1 !

Using types as an array index is a Pascal/Ada family
thing.

Arne

Enums have always been the poor cousin in C/C++ and apparently C#.
 
Well, seeing as how what you want to do is so obvious, maybe you'd like
to
explain what you want to do so that we, who are obviously deficient in
the
area of ESP, can get an idea.

A not uncommon thing I would have thought.

class enAttributes
{
string description;
int somethingelse;
}

enAttributes[] attr = new enAttributes [ en ];

enAttributes a1 = attr [ i1 ];

i1 has a value, I can't see why the compiler can't do the necessary
type-casting.
Ian Semmel said:
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];
 
Well, seeing as how what you want to do is so obvious, maybe you'd

Ian said:
A not uncommon thing I would have thought.

Seems uncommon to me.
class enAttributes
{
string description;
int somethingelse;
}

enAttributes[] attr = new enAttributes [ en ];

enAttributes a1 = attr [ i1 ];

i1 has a value, I can't see why the compiler can't do the necessary
type-casting.

Because it would break type safety?
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

en is the Enum name. It would break type safety to interpret a type name as
an int.
It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

Seems good enough to me.
 
Ian said:
Enums have always been the poor cousin in C/C++ and apparently C#.

The C# enum is a "poor cousin", just because it won't break type safety for
your convenience?

I think not.
 
enAttributes[] attr = new enAttributes [ en ];

enAttributes a1 = attr [ i1 ];

i1 has a value, I can't see why the compiler can't do the necessary
type-casting.

Because i1 is *not* an integer, it's an enum. If you want to create a
map from enum values to something else, use Dictionary<K,V>.
 
Lew said:
The C# enum is a "poor cousin", just because it won't break type safety for
your convenience?

I think not.

In particular, there are plenty of *other* ways in which C# enums are
poor cousins. Java 5's enums still have something to be desired, but
they're a hell of a lot better than C#'s :)
 
Jon said:
In particular, there are plenty of *other* ways in which C# enums are
poor cousins. Java 5's enums still have something to be desired, but
they're a hell of a lot better than C#'s :)

Would you care to elucidate? I'm coming from the Java world just learning C#.
How do C# enums fall short?
 
Lew said:
Would you care to elucidate? I'm coming from the Java world just
learning C#. How do C# enums fall short?

C# enums are just int's in disguise.

I don't think it is a problem.

But you can misuse it.

The following compiles fine:

using System;

namespace E
{
public enum Fruits { Apples, Oranges, Grape };
public enum Cars { Ford, Toyota, Mercedes };
public enum Colors { Red, Green, Blue };
public class TestClass
{
public static void Main(string[] args)
{
Fruits fru = Fruits.Grape;
Cars car = Cars.Mercedes;
Colors col = (Colors)((int)fru / (int)car);
Console.WriteLine(col);
}
}
}

Arne
 
Lew said:
The C# enum is a "poor cousin", just because it won't break type safety
for your convenience?

I think not.

I think his point is the opposite.

He want to make the app more type safe by using enums as
array indexes.

Arne
 
-----Original Message-----
From: Lew [mailto:[email protected]]
Posted At: Monday, 10 December 2007 6:40 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: C# Enums - Feature or deficiency
Subject: Re: C# Enums - Feature or deficiency

Ian said:
A not uncommon thing I would have thought.

Seems uncommon to me.
class enAttributes
{
string description;
int somethingelse;
}

enAttributes[] attr = new enAttributes [ en ];

enAttributes a1 = attr [ i1 ];

i1 has a value, I can't see why the compiler can't do the necessary
type-casting.

Because it would break type safety?

So what does (int) i1 mean ? It shouldn't really be allowed but the
language assumes an ordinal set.

enum Fruit
{
apples,
oranges
}

Allowing int n = (int) oranges + 3 is not correct imo and is meaningless.
It assumes a knowledge of the inner workings of the compiler.

Perhaps the C# people should have looked at pascal as well as C++.
 
Arne Vajhøj said:
I think his point is the opposite.

He want to make the app more type safe by using enums as
array indexes.

The problem with that is that array indexes are always integers.

How can you index some array with an arbitrary object? You can associate an
object with an array element but that is different.
 
Arne Vajh?j said:
I think his point is the opposite.

He want to make the app more type safe by using enums as
array indexes.

That screams to me "Dictionary" - array is (logically) just a special
type of map which always uses ints as the key. At the point where you
want to use a type *other* than int, you should use the more general
form. It will give you additional type safety by not only *allowing*
you to use the enum, but *forcing* you to.
 
Ian Semmel said:
So what does (int) i1 mean ? It shouldn't really be allowed but the
language assumes an ordinal set.

No, the language *defines* an ordinal set.
enum Fruit
{
apples,
oranges
}

Allowing int n = (int) oranges + 3 is not correct imo and is meaningless.
It assumes a knowledge of the inner workings of the compiler.

No it doesn't - it assumes knowledge of the C# language specification,
which lays it out very clearly. No compiler internals are required.
Perhaps the C# people should have looked at pascal as well as C++.

Given that Anders, father of Delphi, designed C#, I think he knows
Pascal pretty well.

It's actually a CLR issue more than C# - the CLR support for enum is
just as labelled numbers, effectively. Very un-OO in my view. Does
Pascal allow enums to have proper behaviour though, in the same way as
Java enums do?
 
Back
Top