//******************************
// Written by Peter Golde
// Copyright (c) 2004-2007, Wintellect
//
// Use and restribution of this code is subject to the license agreement
// contained in the file "License.txt" accompanying this file.
//******************************
using System;
using System.Collections;
using System.Collections.Generic;
namespace Wintellect.PowerCollections
{
/// <summary>
/// A holder class for various internal utility functions that need to be shared.
/// </summary>
internal static class Util
{
/// <summary>
/// Wrap an enumerable so that clients can't get to the underlying
/// implementation via a down-case
/// </summary>
/// <param name="wrapped">Enumerable to wrap.</param>
/// <returns>A wrapper around the enumerable.</returns>
public static IEnumerable<T> CreateEnumerableWrapper<T>(IEnumerable<T> wrapped)
{
return new WrapEnumerable<T>(wrapped);
}
/// <summary>
/// Wrap an enumerable so that clients can't get to the underlying
/// implementation via a down-cast.
/// </summary>
class WrapEnumerable<T> : IEnumerable<T>
{
IEnumerable<T> wrapped;
/// <summary>
/// Create the wrapper around an enumerable.
/// </summary>
/// <param name="wrapped">IEnumerable to wrap.</param>
public WrapEnumerable(IEnumerable<T> wrapped)
{
this.wrapped = wrapped;
}
public IEnumerator<T> GetEnumerator()
{
return wrapped.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)wrapped).GetEnumerator();
}
}
}
}
Format enum value
using System;
enum Color {Yellow = 1, Blue, Green};
class Sample
{
public static void Main()
{
Console.WriteLine("Standard Enumeration Format Specifiers");
Console.WriteLine(
"(G) General:. . . . . . . . . {0:G}
" +
" (default):. . . . . . . . {0} (default = 'G')
" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)
" +
"(D) Decimal number: . . . . . {0:D}
" +
"(X) Hexadecimal:. . . . . . . {0:X}
",
Color.Green);
}
}
switch statement with Enum and call its ToString function
using System;
enum EmpType : byte {
Manager = 10,
Grunt = 1,
Contractor = 100,
VP = 9
}
class Program {
public static void AskForBonus(EmpType e) {
switch (e) {
case EmpType.Contractor:
Console.WriteLine("1");
break;
case EmpType.Grunt:
Console.WriteLine("2");
break;
case EmpType.Manager:
Console.WriteLine("3");
break;
case EmpType.VP:
Console.WriteLine("4");
break;
default: break;
}
}
static void Main(string[] args) {
EmpType fred;
fred = EmpType.VP;
AskForBonus(fred);
Console.WriteLine(fred.ToString());
}
}
Get underlying type
using System;
enum EmpType : byte {
Manager = 10,
Grunt = 1,
Contractor = 100,
VP = 9
}
class Program {
static void Main(string[] args) {
EmpType fred;
fred = EmpType.VP;
Console.WriteLine(Enum.GetUnderlyingType(typeof(EmpType)));
}
}
Enum.Format()
using System;
enum EmpType : byte {
Manager = 10,
Grunt = 1,
Contractor = 100,
VP = 9
}
class Program {
static void Main(string[] args) {
EmpType fred;
fred = EmpType.VP;
Console.WriteLine("You are a {0}", fred.ToString());
Console.WriteLine("Hex value is {0}", Enum.Format(typeof(EmpType), fred, "x"));
Console.WriteLine("Int value is {0}", Enum.Format(typeof(EmpType), fred, "D"));
}
}
Get Enum's type, hex and value.
using System;
enum EmpType : byte {
Manager = 10,
Grunt = 1,
Contractor = 100,
VP = 9
}
class Program {
static void Main(string[] args) {
EmpType fred;
fred = EmpType.VP;
Console.WriteLine("You are a {0}", fred.ToString());
Console.WriteLine("Hex value is {0}", Enum.Format(typeof(EmpType), fred, "x"));
Console.WriteLine("Int value is {0}", Enum.Format(typeof(EmpType), fred, "D"));
}
}
Enum Parse
using System;
enum EmpType : byte {
Manager = 10,
Grunt = 1,
Contractor = 100,
VP = 9
}
class Program {
static void Main(string[] args) {
EmpType fred;
fred = EmpType.VP;
EmpType sally = (EmpType)Enum.Parse(typeof(EmpType), "Manager");
Console.WriteLine("Sally is a {0}", sally.ToString());
}
}

