/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
public class EnumValues
{
// declare the enumeration
enum Temperatures
{
WickedCold = 0,
FreezingPoint = 32,
LightJacketWeather = 60,
SwimmingWeather = 72,
BoilingPoint = 212,
}
static void Main( )
{
System.Console.WriteLine("Freezing point of water: {0}",
(int) Temperatures.FreezingPoint );
System.Console.WriteLine("Boiling point of water: {0}",
(int) Temperatures.BoilingPoint );
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Enum by IEnumerable
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
namespace Enumeration
{
using System;
using System.Collections;
// implements IEnumerable
class ListBoxTest : IEnumerable
{
private string[] strings;
private int ctr = 0;
// private nested implementation of ListBoxEnumerator
private class ListBoxEnumerator : IEnumerator
{
// member fields of the nested ListBoxEnumerator class
private ListBoxTest currentListBox;
private int index;
// public within the private implementation
// thus, private within ListBoxTest
public ListBoxEnumerator(ListBoxTest currentListBox)
{
// a particular ListBoxTest instance is
// passed in, hold a reference to it
// in the member variable currentListBox.
this.currentListBox = currentListBox;
index = -1;
}
// Increment the index and make sure the
// value is valid
public bool MoveNext()
{
index++;
if (index >= currentListBox.strings.Length)
return false;
else
return true;
}
public void Reset()
{
index = -1;
}
// Current property defined as the
// last string added to the listbox
public object Current
{
get
{
return(currentListBox[index]);
}
}
} // end nested class
// Enumerable classes can return an enumerator
public IEnumerator GetEnumerator()
{
return (IEnumerator) new ListBoxEnumerator(this);
}
// initialize the listbox with strings
public ListBoxTest(params string[] initialStrings)
{
// allocate space for the strings
strings = new String[8];
// copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}
// add a single string to the end of the listbox
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}
// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}
// publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}
}
public class EnumerationTester
{
public void Run()
{
// create a new listbox and initialize
ListBoxTest currentListBox =
new ListBoxTest("Hello", "World");
// add a few strings
currentListBox.Add("Who");
currentListBox.Add("Is");
currentListBox.Add("John");
currentListBox.Add("Galt");
// test the access
string subst = "Universe";
currentListBox[1] = subst;
// access all the strings
foreach (string s in currentListBox)
{
Console.WriteLine("Value: {0}", s);
}
}
[STAThread]
static void Main()
{
EnumerationTester t = new EnumerationTester();
t.Run();
}
}
}
Demonstrate an enumeration
using System;
public class EnumDemo {
enum apple { Jonathan, GoldenDel, RedDel, Winsap,
Cortland, McIntosh };
public static void Main() {
string[] color = {
“Red”,
“Yellow”,
“Red”,
“Red”,
“Red”,
“Redish Green”
};
apple i;
for(i = apple.Jonathan; i <= apple.McIntosh; i++) Console.WriteLine(i + " has value of " + (int)i); Console.WriteLine(); for(i = apple.Jonathan; i <= apple.McIntosh; i++) Console.WriteLine("Color of " + i + " is " + color[(int)i]); } } [/csharp]
Enum declare and use it
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_3___Structs__Enums__Arrays_and_Classes
{
enum DaysOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
public class EnumsChapter_3___Structs__Enums__Arrays_and_Classes
{
static void Main(string[] args)
{
DaysOfWeek Today = DaysOfWeek.Monday;
Console.WriteLine(Today);
}
}
}
Enumeration Base Types
enum SmallEnum : byte
{
A,
B,
C,
D
}
Enumerations Initialization 2
enum Values
{
A = 1,
B = 2,
C = A + B,
D = A * C + 33
}
Enumerations:Initialization
public enum Values
{
A = 1,
B = 2,
C = A + B,
D = A * C + 33
}
public class EnumerationsInitialization2
{
public static void Member(Values value)
{
// do some processing here
}
public static void Main()
{
Values value = 0;
Member(value);
}
}