C#语言学习--基础部分(十三)枚举类型和结构体

枚举类型和结构体都是属于值类型,他们的值保存在栈中.

1.枚举ConsoleDemo

namespace EnumDemo
{
    enum Sean { Spring,Summer,Fall,Winnter}
    class Program
    {
        static void Main(string[] args)
        {
            Sean s = Sean.Fall;
            Console.WriteLine(s);
        }
    }
}

 2.结构类型ConsoleDemo

namespace StructsDemo
{
    struct Time {
        int hours, minutes, seconds;
        public Time(int hh, int mm, int ss)
        {
            hours = hh;
            minutes = mm;
            seconds = ss;
        }
        public Time(int hh, int mm) //和类有区别,类不需要设置seconds.
        {
            hours = hh;
            minutes = mm;
            seconds = 0//必须显示的设置seconds为零.

        }
        public int Hours() { 
            return hours;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Time t = new Time(); //不声明也可以调用默认构造函数,和类有所不同,类若是提供了构造方法,则系统不再提供默认构造方法
            Console.WriteLine(t.Hours());
            Time t2 = new Time(10,20,30);
            Console.WriteLine(t2.Hours());
            Time t3 = new Time(50,50);
            Console.WriteLine(t3.Hours());
        }
    }
}

 3.枚举类型和结构体ConsoleDemo

namespace StructsEnums
{
    struct Date {
        private int year;
        private Month month;
        private int day;
        public Date(int ccyy,Month mm,int dd) {
            this.year = ccyy - 1900;
            this.month = mm;
            this.day = dd -1;
        }
        public override string ToString()
        {
            string data = String.Format("{0} {1} {2}",this.month,this.day+1,this.year+1900);
            return data;
        }
    }
    enum Month {
        January,February,March,April,May,June,July,August,Septempber,October,November,December
    }
    class Program
    {
        static void Main(string[] args)
        {
            doWork();
        }
        static void doWork()
        {
            Month first = Month.January;
            first = Month.December;
            Console.WriteLine(first);
            first++;
            Console.WriteLine(first);
            Date defaultDate = new Date();
            Console.WriteLine(defaultDate);
            Date currentDate = new Date(2012, Month.August, 25);
            Console.WriteLine(currentDate);
        }
    }
}

 

posted on 2012-08-26 00:03  松波  阅读(194)  评论(0编辑  收藏  举报

导航