[C#基础]7.C#中的switch和其他语言的不同

Posted on 2009-09-26 12:38  Relax Active  阅读(265)  评论(0)    收藏  举报
 

学过C和C++朋友应该对switch…case语句很熟悉,这里讨论他们的不同之处。

1. C#switch…case语句比其它语言更安全;

2. C#禁止所有case的失败条件;

3. C#支持goto语句,可使用goto语句激活后面的case子句;

4. C#中的case子句可任意排序;

5. C#中可以把字符串用做测式变量!

代码示例:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Demo7

{

    class Program

    {

        static void Main(string[] args)

        {

            int integerA = 2;

            switch (integerA)

            {

                case 1:

                    Console.WriteLine(integerA = 1);

                    break;

                case 2:

                    Console.WriteLine(integerA = 2);

                    break;

                case 3:

                    Console.WriteLine(integerA = 3);

                    break;

                default:

                    Console.WriteLine("integerA is not 1 ,2 ,or 3");

                    break;

            }

        }

    }

}

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3