using System;
using System.Collections.Generic;
using System.Text;
class Program
{
//Flag enums are designed to support bitwise operations on the enum values
[Flags]
public enum LogLevels : uint
{
//Increment by powers of 2.
//If you don't assign explicit values VS increments by 1.
None = 0,
NotifyTrace = 1,
NotifyDebug = 2,
NotifyInfo = 4,
NotifyWarn = 8,
NotifyError = 16,
NotifyAll = NotifyTrace | NotifyDebug | NotifyInfo | NotifyWarn | NotifyError,
//The rationale for avoiding zero in a flag enumeration for an actual flag
//is that you can't OR it in with other flags as expected.
NotifyNone = ~(NotifyTrace | NotifyDebug | NotifyInfo | NotifyWarn | NotifyError),
}
LogLevels options = LogLevels.NotifyDebug | LogLevels.NotifyWarn | LogLevels.NotifyError;
static void Main(string[] args)
{
Program p = new Program();
p.Test1();
p.Test2();
LogLevels v = (LogLevels) 2;
p.Test3(v);
// Interesting, Compiler Catch Invalid Argument
p.Test4(2);
// Surprise, Correct
p.Test4(0);
Console.ReadLine();
}
public void Test1()
{
Type value = typeof(LogLevels);
foreach (string s in Enum.GetNames(value))
{
Console.WriteLine("{0,-11} = {1}", s, Enum.Format(value, Enum.Parse(value, s), "d"));
}
}
public void Test2()
{
Console.WriteLine(IsLevelsSet(LogLevels.NotifyNone));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyTrace));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyDebug));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyInfo));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyWarn));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyError));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyAll));
}
public void Test3(LogLevels value)
{
switch (value)
{
case LogLevels.NotifyTrace:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyDebug:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyInfo:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyWarn:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyError:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyAll:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyNone:
Console.WriteLine(value.ToString());
break;
default:
break;
}
}
public void Test4(LogLevels value)
{
//in C# the literal constant 0 implicitly converts to any enum type
if (value == LogLevels.None)
Console.WriteLine(value.ToString());
}
private bool IsLevelsSet(LogLevels values)
{
return (options & values) == values;
}
}
using System.Collections.Generic;
using System.Text;
class Program
{
//Flag enums are designed to support bitwise operations on the enum values
[Flags]
public enum LogLevels : uint
{
//Increment by powers of 2.
//If you don't assign explicit values VS increments by 1.
None = 0,
NotifyTrace = 1,
NotifyDebug = 2,
NotifyInfo = 4,
NotifyWarn = 8,
NotifyError = 16,
NotifyAll = NotifyTrace | NotifyDebug | NotifyInfo | NotifyWarn | NotifyError,
//The rationale for avoiding zero in a flag enumeration for an actual flag
//is that you can't OR it in with other flags as expected.
NotifyNone = ~(NotifyTrace | NotifyDebug | NotifyInfo | NotifyWarn | NotifyError),
}
LogLevels options = LogLevels.NotifyDebug | LogLevels.NotifyWarn | LogLevels.NotifyError;
static void Main(string[] args)
{
Program p = new Program();
p.Test1();
p.Test2();
LogLevels v = (LogLevels) 2;
p.Test3(v);
// Interesting, Compiler Catch Invalid Argument
p.Test4(2);
// Surprise, Correct
p.Test4(0);
Console.ReadLine();
}
public void Test1()
{
Type value = typeof(LogLevels);
foreach (string s in Enum.GetNames(value))
{
Console.WriteLine("{0,-11} = {1}", s, Enum.Format(value, Enum.Parse(value, s), "d"));
}
}
public void Test2()
{
Console.WriteLine(IsLevelsSet(LogLevels.NotifyNone));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyTrace));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyDebug));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyInfo));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyWarn));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyError));
Console.WriteLine(IsLevelsSet(LogLevels.NotifyAll));
}
public void Test3(LogLevels value)
{
switch (value)
{
case LogLevels.NotifyTrace:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyDebug:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyInfo:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyWarn:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyError:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyAll:
Console.WriteLine(value.ToString());
break;
case LogLevels.NotifyNone:
Console.WriteLine(value.ToString());
break;
default:
break;
}
}
public void Test4(LogLevels value)
{
//in C# the literal constant 0 implicitly converts to any enum type
if (value == LogLevels.None)
Console.WriteLine(value.ToString());
}
private bool IsLevelsSet(LogLevels values)
{
return (options & values) == values;
}
}
No comments:
Post a Comment