引用类型有类和接口
值类型有基元类型(primitive type)、结构和枚举
所有值类型都必须从System.ValueType派生
(System.ValueType直接从System.Object派生)
所有值类型都隐式密封(不派生)。值类型对象有两种表示形式:未装箱和已装箱;
引用类型总是处于已装箱形式。
枚举类型(enumerated type)定义了一组“符号名称/值”配对。
1)枚举类型使程序更容易编写、阅读和维护;
2)枚举类型是强类型
枚举类型直接从System.Enum派生,System.Enum-》System.ValueType-》System.Object
伪类型枚举类型只是一个结构,其中定义了一组常量字段和一个实例字段。
C#编译器将枚举类型视为基元类型(primitive type)。
定义的枚举的类型应该和需要它的类型同级。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyEnum
{
/* internal enum Color
{
White,
Red,
Green,
Blue,
Orange
}
internal struct Color : System.Enum
{
// Following are public constants defining Color's symbols and values
public const Color White = (Color)0;
public const Color Red = (Color)1;
public const Color Green = (Color)2;
public const Color Blue = (Color)3;
public const Color Orange = (Color)4;
// Following is a public instance field containing a Color variable's value
// You cannot write code that references this instance field directly
public Int32 value__;
}
*/
internal enum Color : byte
{
White,
Red,
Green,
Blue,
Orange
}
[Flags] // The C# compiler allows either "Flags" or "FlagsAttribute".
internal enum Actions {
None = 0,
Read = 0x0001,
Write = 0x0002,
ReadWrite = Actions.Read | Actions.Write,
Delete = 0x0004,
Query = 0x0008,
Sync = 0x0010
}
class Program
{
public static TEnum[] GetEnumValues<TEnum>() where TEnum : struct
{
return (TEnum[])Enum.GetValues(typeof(TEnum));
}
static void Main(string[] args)
{
// The following line displays "System.Byte".
Console.WriteLine(Enum.GetUnderlyingType(typeof(Color)));
Color c = Color.Blue;
Console.WriteLine(c); // "Blue" (General format)
Console.WriteLine(c.ToString()); // "Blue" (General format)
Console.WriteLine(c.ToString("G")); // "Blue" (General format)
Console.WriteLine(c.ToString("D")); // "3" (Decimal format)
Console.WriteLine(c.ToString("X")); // "03" (Hex format)
Color[] colors = (Color[])Enum.GetValues(typeof(Color));
Console.WriteLine("Number of symbols defined: " + colors.Length);
Console.WriteLine("Value\tSymbol\n-----\t------");
foreach (Color c1 in colors)
{
// Display each symbol in Decimal and General format
Console.WriteLine("{0, 5:D}\t{0:G}", c1);
}
string[] colSymbol = Enum.GetNames(typeof(Color));
Console.WriteLine("Number of symbols defined: " + colSymbol.Length);
foreach (string sym in colSymbol)
{
// Display each symbol in Decimal and General format
Console.WriteLine(sym);
}
// Because Orange is defined as 4, 'c' is initialized to 4.
Color c2 = (Color)Enum.Parse(typeof(Color), "orange", true);
// Creates an instance of the Color enum with a value of 23
Enum.TryParse<Color>("23", false, out c);
Actions actions = Actions.Read | Actions.Delete; // 0x0005
Console.WriteLine(actions.ToString()); // "Read, Delete"
Console.WriteLine(actions.ToString("F")); // "Read, Delete"
// Because Query is defined as 8, 'a' is initialized to 8.
Actions a = (Actions)Enum.Parse(typeof(Actions), "Query", true);
Console.WriteLine(a.ToString()); // "Query"
// Because Query and Read are defined, 'a' is initialized to 9.
Enum.TryParse<Actions>("Query, Read", false, out a);
Console.WriteLine(a.ToString()); // "Read, Query"
// Creates an instance of the Actions enum with a value of 28
a = (Actions)Enum.Parse(typeof(Actions), "28", false);
Console.WriteLine(a.ToString()); // "Delete, Query, Sync"
Console.ReadKey();
}
}
}