zoukankan      html  css  js  c++  java
  • C#3.0学习笔记(5)浅谈枚举

    1, 什么叫枚举?

           答:枚举是由程序员定义的类型,与类或结构一样。

           注:1> 与结构一样,枚举是值类型,因此直接存储它们的数据,而不是分                           开存储成引用和数据。枚举存储在栈中。

               2> 枚举只有一种类型的成员:命名的整数值常量。

               3> 每个枚举类型都有一个底层整数类型,默认为int。编译器把第一个              成员赋值为0,并对每一个后续成员赋的值比前一个成员多1。

    2, 枚举在栈中排列的示例?

           class Program

    {

    static voidMain(string[] args)

    {

    TrafficLight t1 = TrafficLight.Green;

    TrafficLight t2 = TrafficLight.Yellow;

    TrafficLight t3 = TrafficLight.Red;

    Console.WriteLine("{0},{1}", t1,(int)t1);

    Console.WriteLine("{0},{1}", t2, (int)t2);

    Console.WriteLine("{0},{1}", t3, (int)t3);

    Console.ReadKey();

    }

    }

    enum TrafficLight

    {

    Green,

    Yellow,

    Red

    }

           程序输出的结果为:

           Green,0

           Yellow,1

           Red,2

    3, 关于枚举的补充?

           因为枚举的成员是常量,即使在没有该枚举类型的变量时它们也可以访问。使用枚举    类型名跟着一个点和成员名。

           示例代码:

           class Program

    {

    static voidMain(string[] args)

    {

    Console.WriteLine("{0}", TrafficLight.Green);

    Console.WriteLine("{0}", TrafficLight.Yellow);

    Console.WriteLine("{0}", TrafficLight.Red);

    Console.ReadKey();

    }

    }

    enum TrafficLight

    {

    Green,

    Yellow,

    Red

    }

           程序输出结果:

           Green

           Yellow

           Red

  • 相关阅读:
    扇入扇出的概念
    ISE Simulator综合后仿真 How do you run Post Synthesis Simulation in ISE Project Navigator?
    通信相关经典书籍
    the advantages of using SRL16 to make large delay
    Illegal redeclaration of module <glbl>.&Element index 2 into memp is out of bounds
    双口RAM
    twisted综述和reactor概述
    python os模块
    Twisted简单安装和rpc简单使用示例
    select, poll和epoll的区别
  • 原文地址:https://www.cnblogs.com/mcgrady/p/2228856.html
Copyright © 2011-2022 走看看