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);
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
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());
}
}
Get all stats for EmpType.
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;
Array obj = Enum.GetValues(typeof(EmpType));
Console.WriteLine("This enum has {0} members:", obj.Length);
}
}