示例#1
0
        public static void Main()
        {
            //用枚举类型Weekday声明变量并赋初值
            Weekday wd1 = Weekday.Mon;

            Console.WriteLine("每周的工作日从{0}开始", wd1);
            Weekday wd2 = Weekday.Fri;

            Console.WriteLine("到{0}结束", wd2);

            //枚举类型变量用于计算
            Console.WriteLine("每周有{0}个工作日", wd2 - wd1 + 1);
            Console.WriteLine();

            //枚举类型变量作为函数的参数
            Console.WriteLine("Mon之后是{0}", NextDay(Weekday.Mon));
            Console.WriteLine();

            //使用枚举类型的GetNames()方法
            foreach (string s1 in Weekday.GetNames(typeof(Weekday)))
            {
                Console.Write(s1 + " ");
            }
            Console.WriteLine();

            //从命令行读取枚举类型变量的值
            Console.Write("今天是星期几?");
            Weekday wd3 = (Weekday)Weekday.Parse(typeof(Weekday), Console.ReadLine());

            Console.WriteLine("明天是{0}", NextDay(wd3));

            //比较枚举类型的值
            Console.Write("体育课在星期几?");
            wd1 = (Weekday)Weekday.Parse(typeof(Weekday), Console.ReadLine());
            Console.Write("物理实验在星期几?");
            wd2 = (Weekday)Weekday.Parse(typeof(Weekday), Console.ReadLine());
            if (wd2.CompareTo(wd1) == 0)
            {
                Console.WriteLine("糟糕,在同一天");
            }
            else
            {
                Console.WriteLine("没事,不在同一天");
            }

            Console.ReadLine();
        }
示例#2
0
        public static void Main()
        {
            // 用枚举类型Weekday声明变量并赋初值
            Weekday wd1 = Weekday.Mon;

            Console.WriteLine("每周的工作日从{0}开始", wd1);
            Weekday wd2 = Weekday.Fri;

            Console.WriteLine("到{0}结束", wd2);

            // 枚举类型变量用于计算
            Console.WriteLine("每周有{0}个工作日", wd2 - wd1 + 1);
            Console.WriteLine();

            // 枚举类型变量作为函数的参数
            Console.WriteLine("Mon之后是{0}", NextDay(Weekday.Mon));
            Console.WriteLine();

            // 使用枚举类型的GetNames()方法
            foreach (string s1 in Weekday.GetNames(typeof(Weekday)))        //Enum.GetName(Type enumtype)检索枚举中常数名称的数组
            {
                Console.Write(s1 + " ");
            }
            Console.WriteLine("\n");

            // 从命令行读取枚举类型变量的值
            Console.Write("今天是星期几? ");
            Weekday wd3 = (Weekday)Weekday.Parse(typeof(Weekday), Console.ReadLine());      //Enum.Parse(Type enumtype,string value)将字符串转换成等效的枚举对象

            Console.WriteLine("明天是{0}", NextDay(wd3));
            Console.WriteLine();

            // 比较枚举类型的值
            Console.Write("体育课在星期几? ");
            wd1 = (Weekday)Weekday.Parse(typeof(Weekday), Console.ReadLine());
            Console.Write("物理试验在星期几? ");
            wd2 = (Weekday)Weekday.Parse(typeof(Weekday), Console.ReadLine());
            if (wd1.CompareTo(wd2) == 0)                                                //Enum.CompareTo(Enum target)    比较对象
            {
                Console.WriteLine("糟糕,在同一天。");
            }
            else
            {
                Console.WriteLine("没事,不在同一天");
            }
        }
示例#3
0
    public int CompareTo(WeeklyEntry other)
    {
        if (ReferenceEquals(this, other))
        {
            return(0);
        }
        if (ReferenceEquals(null, other))
        {
            return(1);
        }
        var weekdayComparison = Weekday.CompareTo(other.Weekday);

        if (weekdayComparison != 0)
        {
            return(weekdayComparison);
        }
        return(string.Compare(Notes, other.Notes, StringComparison.Ordinal));
    }
示例#4
0
        public static void Test3()
        {
            // 分别输出四个方向和对应的整数值
            Console.WriteLine(MyDirection.East);
            Console.WriteLine(MyDirection.South);
            Console.WriteLine(MyDirection.West);
            Console.WriteLine(MyDirection.North);
            Console.WriteLine(Convert.ToInt32(MyDirection.East));
            Console.WriteLine(Convert.ToInt32(MyDirection.South));
            Console.WriteLine(Convert.ToInt32(MyDirection.West));
            Console.WriteLine(Convert.ToInt32(MyDirection.North));

            // 使用枚举类型的GetNames()方法
            foreach (string s1 in Enum.GetNames(typeof(Weekday)))
            {
                Console.Write(s1 + " ");
            }
            Console.WriteLine("\n");

            // 从命令行读取枚举类型变量的值
            Console.Write("今天是星期几? ");
            Weekday wd3 = (Weekday)Enum.Parse(typeof(Weekday), Console.ReadLine());

            Console.WriteLine("明天是{0}", NextDay(wd3));
            Console.WriteLine();

            // 比较枚举类型的值
            Console.Write("体育课在星期几? ");
            Weekday wd1 = (Weekday)Enum.Parse(typeof(Weekday), Console.ReadLine());

            Console.Write("物理试验在星期几? ");
            Weekday wd2 = (Weekday)Enum.Parse(typeof(Weekday), Console.ReadLine());

            if (wd1.CompareTo(wd2) == 0)
            {
                Console.WriteLine("糟糕,在同一天。");
            }
            else
            {
                Console.WriteLine("没事,不在同一天");
            }
        }
    public int CompareTo(WeeklyEntry other)
    {
        var result = 0;

        if (ReferenceEquals(this, other))
        {
            result = 0;
        }
        else if (ReferenceEquals(null, other))
        {
            result = 1;
        }
        else
        {
            result = Weekday.CompareTo(other.Weekday);
        }

        if (result == 0)
        {
            result = string.Compare(this.Notes, other.Notes, StringComparison.Ordinal);
        }

        return(result);
    }