C#值类型

引用类型有类和接口

值类型有基元类型(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();
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值