本帖最后由 熊薇 于 2013-4-18 16:53 编辑
- enum Orientation : byte
- {
- north = 1,
- south,
- east,
- west
- }
- static void Main(string[] args)
- {
- Orientation myDirection;
- for (byte myByte = 2; myByte < 6; myByte++)
- {
- try
- {
- myDirection=checked((Orientation)myByte); //checked什么意思?
- if(myDirection<Orientation.north||myDirection>Orientation.west)
- {
- throw new ArgumentOutOfRangeException("myByte",myByte,
- "Value must be between 1 and 4");
- }
- }
- catch(ArgumentOutOfRangeException e)
- {
- Console.WriteLine(e.Message);
- Console.WriteLine("Assigning default value,Orientation.north ");
- myDirection = Orientation.north;
- }
- Console.WriteLine("myDirection={0}", myDirection);
- }
- }
复制代码 |