enum keyword
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
enum keyword
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  unchecked keyword [next Lesson]  base keyword
enum keyword
  by: Salman Ahmed

enum

The enum keyword is used when you require a enumeration. A enumeration is a distinct type that consists of a set of named constants called the enumetor list. Every enumeration type has an underlying type which can be all integral times except that of a char type.

Declaration syntax:

[attributes] [modifiers] enum identifier [:base-type] {enumerator-list};
Where:
  • attributes (optional) - this is additional declarative information, look under index A for more information
  • modifiers (optional) - permissible modifiers include: new, public, protected, internal and private ( the 4 access modifiers)
  • base-type (optional) - underlying type for the storage allocated for each enumerator, any integral type except char (default is int)
  • enumerator-list - the enumerators identifiers which are seperated by commas (optionally including a value assignment)
Key Points:

The default underlying type of the elements in the enumeration is int as previously mentioned. The first enumerator begins at 0 by default and each enumerator following is increased by 1. Explicit casts are required to convert from enum type to an integral type.



Examples:
// create enum for all of the months in a year
enum Months {Jan, Feb, March, April,
	 May, June, July, August, Sept, Oct, Nov, Dec};

// same as above EXCEPT forced to start sequence 
// from 1 instead of 0 (default)
enum Months2 {Jan = 1, Feb, March, April,
	 May, June, July, August, Sept, Oct, Nov, Dec};
    
public void Page_Load(object sender, EventArgs e)
{
    // notice the explicit cast required
    int x = (int) Months.Feb;
    int y = (int) Months.Oct;
    
    Response.Write("
Feb is month #" + x); Response.Write("
Oct is month #" + y); // sequence forced to start with 1 i.e Jan = 1 (in the declaration) x = (int) Months2.Feb; y = (int) Months2.Oct; Response.Write("
Feb is now month #" + x); Response.Write("
Oct is now month #" + y); }

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
enum keyword
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  unchecked keyword [next Lesson]  base keyword


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
simboy 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center