Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
Enum and Arrays, what is the difference?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.


Shane
Can someone please explain the difference between using an enum and using an
array? It would seem to me that you would always just use an array. Can
someone also explain when you would use an array and when it would be ideal
to use an enum.

TIA,
Shane

Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Nicholas Paldino [.NET MVP]
Shane,

An array is used to store an arbitrary number of items that are the same
type (or derive from the same time).

An enumeration is used to give a lexical representation to a numeric
value, of which there is a fixed set of values to choose from. For example,
a days of the week enumeration would be:

public enum DayOfWeek
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}

The value for Sunday would be 0, Monday would be 1, and so on, and so
on.

Arrays, on the other hand, can contain the same values over and over
again. They are nothing more than containes for multiple items of the same
type.

Hope this helps.

--
- Nicholas Paldino [.NET MVP]
- Click here to reveal e-mail address

"Shane" <Click here to reveal e-mail address> wrote in message
news:OpgXL6m1BHA.1160@tkmsftngp04...
> Can someone please explain the difference between using an enum and using
an
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
Paitum
Enumerations and Arrays are used in very different ways.

An enumeration is a way to create some related constants. For instance, if
you were creating a calendar program, then you might create an enumeration
for the days of the week.

public enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday };

This creates a data-type called Days and 7 constants. In this example
"Days.Sunday" = 0, "Days.Monday" = 1 and so on... This is MUCH better than
just using number values throughout your program. Decide for yourself:
Which of the following code segments make more sense?

Without Enumerations

int today = 5;
if ( today != 0 && today != 4 )
...;

With Enumerations

Days today = Days.Thursday;
if ( today != Days.Sunday && today != Days.Thursday )
...;

If you prefer the top example because it is smaller think about the day that
somebody else might have to read your code.

An Array is data-type that stores many similar objects. If you have ever
written a program that had variables like:

int data1;
int data2;
int data3;

then you'd probably have been better off using an array. The above example
would be:

int[] data = new int[3];

( Arrays can become fairly tricky, and I'd recommend getting a C# book. )
The array version of this data is more flexible than the stand-alone
variables for many reasons. Lets say you had 50 numbers you wanted to
store. Then let's say you wanted to add them all up. Which of the
following code looks good:

Without Arrays

int sum = data1 + data2 + data3 + data4 + data5 + data6 + data7 + data8
+ data9 + data10
+ data11 + data12 + data13 + data14 + data15 + data16 + data17 +
data18 + data19 + data20
+ data21 + data22 + data23 + data24 + data25 + data26 + data27 +
data28 + data29 + data30
+ data31 + data32 + data33 + data34 + data35 + data36 + data37 +
data38 + data39 + data40
+ data41 + data42 + data43 + data44 + data45 + data46 + data47 +
data48 + data49 + data50;

With Arrays

int sum = 0;
for( int x = 0; x < data.length; x++ )
sum = sum + data[x];

Final thought. You can have an Array of enumerations, but NOT an
enumeration of arrays ( doesn't even make sense )

Array of Enumerations

Days[] month = new Days[30];
month[0] = Days.Sunday;
if ( month[1] == Days.Monday )
...;

-- Paitum

"Shane" <Click here to reveal e-mail address> wrote in message
news:OpgXL6m1BHA.1160@tkmsftngp04...
> Can someone please explain the difference between using an enum and using
an
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
-glenn-
Paitum, someone recently pointed this out in a previous thread, but I'll
restate it here. When naming your enums, make them singular. (enum Day
instead of enum Days.) This reads much better everywhere the enum is used.
And it's the convention that .Net uses.

Excellent explanation nonetheless.

-glenn-

"Paitum" <Click here to reveal e-mail address> wrote in message
news:uBqr0Mn1BHA.2272@tkmsftngp02...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Email Archiving and Email Filing - what’s the difference?
Web-based task/todo list management
 
    
Shane
Thanks to everyone who replied. It makes much more sense now.

Shane
"-glenn-" <Click here to reveal e-mail address> wrote in message
news:u7xtPDo1BHA.2660@tkmsftngp05...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Open source windows
The Law Society’s guidelines on e-mail management
 
    
Paitum
You are very right. Shane goto

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/ht
ml/cpconnamingguidelines.asp

to learn about how to properly name things.

-- Paitum

"-glenn-" <Click here to reveal e-mail address> wrote in message
news:u7xtPDo1BHA.2660@tkmsftngp05...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
 
System.DayOfWeek




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734