How to test if an enum is inside a set

  • Thread starter Thread starter Alexander Muylaert
  • Start date Start date
A

Alexander Muylaert

Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I'm looking for something like this in C#.
I can't find it. Anybody has found it already?

Kind regards

Alexander
 
Alexander,

If you are looking to see if the value is in a subset of the values in
an enumeration, then you will have to do it the way you are doing it (or
place the subset in an array and then search through that).

However, if the set you are looking through is the enumeration yourself,
then you can call the static IsDefined method on the Enum class to determine
whether or not the value is in the enumeration.

Hope this helps.
 
Put the FlagsAttribute thingy on the enum, and then have
if((aEnum & (Green | Red | Yellow)) != 0)
....

have to make sure the values of the enum are like 0x01, 0x02, 0x10, 0x20,
etc.
 
Alexander,

There's no direct equivalent. However, something like the following should
make things a bit more convenient:

public bool EnumInList(Enum target, params Enum[] allowedValues)
{
bool found = false;

for (int i = 0; i < allowedValues.Length; i++)
{
if (Enum.Equals(allowedValues, target))
{
found = true;
break;
}
}

return found;
}

For better performance, use your particular enum type rather than the Enum
base class.

HTH,
Nicole
 
IsDefined isn't really a great choice even the desired set covers the enum
values. See http://blogs.msdn.com/brada/archive/2003/11/29/50903.aspx for
details.


Nicholas Paldino said:
Alexander,

If you are looking to see if the value is in a subset of the values in
an enumeration, then you will have to do it the way you are doing it (or
place the subset in an array and then search through that).

However, if the set you are looking through is the enumeration
yourself,
then you can call the static IsDefined method on the Enum class to
determine
whether or not the value is in the enumeration.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Alexander Muylaert said:
Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I'm looking for something like this in C#.
I can't find it. Anybody has found it already?

Kind regards

Alexander
 
Thanks a lot

This is a really, really usefull feature of delphi. I can't figuere out why
they didn't implemented the set behaviour of Delphi in C#.

Kind regards

Alexander
 
Alexander said:
This is a really, really usefull feature of delphi. I can't figuere out why
they didn't implemented the set behaviour of Delphi in C#.

Here's one theory:

include delphi-like sets -->
alienate current c++ users -->
less c++ users switch over to c# -->
microsoft lose profit
 
----- Alexander Muylaert wrote: -----
Thanks a lot
This is a really, really usefull feature of delphi. I can't figuere out why
they didn't implemented the set behaviour of Delphi in C#.

because C# is based on C family syntax, not Delphi syntax I guess.
 
A few reasons:
- Back in the old Pascal days, sets used to be very slow; Although
processors have become much faster, and compilers a lot better, I still
think using them in statements like "if x in [1,2,3]" really hurts
performance
- In your case, using a flag-enum would do exactly the same job (but much
faster)
- The .net framework provides a the Hashtable class that's more flexible
than delphi-sets, as any class may be used as key
- C# - as the name implies - is a relative of C and C++; neither of them
have sets
- Usually, looking at a syntax element should tell you roughly how expensive
it is; Noone expects that "[1]" internally creates an object that 256 bytes
big! (assuming delphi sets are more or less the same as old turbo pascal
sets)

Niki
 
C# Learner said:
Here's one theory:

include delphi-like sets -->
alienate current c++ users -->
less c++ users switch over to c# -->
microsoft lose profit

LOL, it is a nice concept however, so I would hope Microsoft would be more
open minded than that. I'd like to see an isin(it makes more sense than just
in, however in or a spaced keyword is in could be used to spare a keyword)
operator that works across IList, ICollection<T>, arrays, strings, and a few
other bits(wrapper around .Contains for various types mostly). I've been
considering implementing it in Mono just to see how it works, but I havn't
had time(as an offisde, I did implement it in another language and the
concept worked fairly clearly, IMHO). Its easy to declare inline arrays so
that isn't an issue, its just a rather simple keyword. Its mostly usability
and cleanliness reasons to consider, the biggest problem is that it doesn't
operate over one given interface like most keywords do, instead it has to
operate over several(IList, ICollection<T>, and string primarily, though for
the sake of a more functional use it may be appropriate for it to work over
IEnumerable\IEnumerator implementations as well, if none of the other
interfaces exist)
 
using set in Delphi doesn't hurt performance nothing more then

the "in" statement in delphi does nothing more then if ((X & Mask) != 0) .
Since it uses 32-bit arithmetics and AND of the cpu it is not slow. Not
slower then and anyway ;-)

C# is mainly invented by the guy that gave us Delphi. C# actually has more
in common with Delphi and Java than it has in common with C or C++. Except
for the syntax, wich is of no importance.

Kind regards

Alexander
 
Daniel said:
except Anders Hejlsberg (and Microsoft) obviously decided, like java, to model the syntax after C family instead of Pascal/Delphi family.

That's hardly a valid argument here. The people behind C# could've
picked constructs from _any_ language and applied them to C#.

And they _did_ do this, for example with properties. Neither C nor C++
has properties.
 
btw, isn't there something called Delphi.NET available?

If you cut of a chickes head, it will continue walking for a while, but it
will never lay an egg anymore.

That is more or less what happened with Delphi after D4. It kinda never
evolved. Octane is pretty, but it is only pretty because it integrates
..net. It took ms 10 years to catch up with Delphi, but they did one heck of
a job.

But times change and after testing Octane I don't see any advantage over C#.
C# has some very nice features and it is way, way cheaper then Delphi.
($1000 vs $3500). I have to say to that to use a tiny class from Delphi
inside C# you have to include 7000 assemblies with your installation. This
kinda removes all the advantages of 1 framework.

Kind regards

Alexander
 
Beeeeves said:
That's the problem - delphi's just a bit too "twee" for most people.

LOL, twee huh? Wonder how many non-brits know what it means, I had to look
it up.

The problem with delphi has nothign to do with being "twee", its the syntax
that kills it. I would say delphi(and pascal) are probably at the bottom of
my list as far as syntax goes. I'd rather use lisp.

However, I don't see how your response relates to the post in any way, could
you perhaps have quoted so it made sense?
 
What I was on about it being "twee" is exactly as you said - it has
crap syntax (not a single bracket in sight - that can't be good for
the compiler). IOW, the inventor(s) seem(s) to have pandered
to the whims of users who want to write "nice" looking code,
not how it is designed to be easy for the compiler (to optimize)
aswell as to code.
 
Beeeeeves said:
What I was on about it being "twee" is exactly as you said - it has
crap syntax (not a single bracket in sight - that can't be good for
the compiler). IOW, the inventor(s) seem(s) to have pandered
to the whims of users who want to write "nice" looking code,
not how it is designed to be easy for the compiler (to optimize)
aswell as to code.

Hrmm, I don't consider it particluarly nice looking, so I guess thats where
you lost me.
 
Daniel O'Connell [C# MVP] wrote:

[...]
The problem with delphi has nothign to do with being "twee", its the syntax
that kills it. I would say delphi(and pascal) are probably at the bottom of
my list as far as syntax goes. [...]

The first time I saw Delphi/Pascal syntax, I cringed. I hated it
straight away.

I thought I'd try Delphi anyway, since I'd heard great things about the
environment. I ended up loving the language, as I find it to be so nice
and clear.

With C and me, it goes the other way. At first, I loved C's
minimalistic syntax. Everything about it seemed perfect. After using
it for some time, though, I ended up not liking it so much.

It's a case of different people and different tastes, I guess.
 
Beeeeeves said:
What I was on about it being "twee" is exactly as you said - it has
crap syntax (not a single bracket in sight - that can't be good for
the compiler).

Whereas, on the other hand, I don't really like C's braces.

Even though 'begin' and 'end' are too verbose, IMO, I'd much rather have
block specifiers that consist of multiple characters. It makes for more
readable code, IMO.
 
IOW, the inventor(s) seem(s) to have pandered
to the whims of users who want to write "nice" looking code,
not how it is designed to be easy for the compiler (to optimize)
aswell as to code.

Euh, you're joking right? What is the difference of "begin" or "{" as token
for the optimizer?

begin
DoThing();
end;

{
DoThing();
}

Honestly, if you believe that the first syntax is less optimizable then the
second, then I can advise you a great book of Steven S. Muchnick. Advanced
compiler design. Perhaps the parses looses some ticks because the tokens in
pascal are multicharacter. But, trust me, your cpu doesn't care if you
assigned with a := or with a =. MOV is MOV and that's it.

btw, are you aware that Delphi is a 1 pass compiler where as C/C++ are two
pass compilers.

It is true, that delphi doesn't allow you to write something like this

if (MyTempVar = (++Counter++ == 5)) {
Counter += ++MyTempVar-- != 2;
}

Perfectly valid syntax in C/C++. But I think it is safely to say that even
a C-die-hard manager would fire any guy that codes this way. I like pascal
since it forces you to use multiple statements. It keeps code easier to
track.

But, apart from the syntax. Delphi has some really nice features I miss in
C# and vica versa.

Kind regards

Alexander
 

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

Back
Top