C#异常处理c#是一种现代、简单和通用的编程语言。它是一种基于c++的面向对象语言。c#具有与Java相似的特性。它不直接支持多重继承。然而,c#使用接口来实现多重继承。它是微软。net计划的一部分,被设计成平台独立的。c#程序被编译成Microsoft中级语言(MSIL)。对于C、c++和Java开发人员来说,学习c#相对容易。这种语言的一个重要特性是垃圾收集。c#支持核心的面向对象的概念,如封装、继承和多态性。

在这个中级教程中,我们将带领你学习c#的select case。我们假设您熟悉c#的基础知识。否则你可以选修这门课来学习c#编程的基础。

一个简单的c#程序

使用系统;/*介绍c#程序*/ Console. conf . conf . conf。WriteLine(“欢迎大家”);}}

让我们以一个简单的c#程序为例,让您开始学习c#的基本构造。用c#编写任何程序都需要它。using关键字在程序中包括System名称空间。一个c#程序可能有多个using语句。命名空间包含类。命名空间ExampleApplication包含类Example。class关键字将Example声明为一个新类。根据定义,类由数据和方法组成。类可以包含多个方法。c#程序的执行从main()函数开始。 Comments in this language start with ‘/* and end with ‘*/’. The compiler leaves the comments untouched. The static keyword ensures that there is only one instance of main() method which is class specific. Objects created with this class will not have object specific instances of the main() method. The main() method’s return type is void. This means the method will not return any value. Inside the main() method the class Console call its member function WriteLine to print the string “Welcome Everybody” on the output screen.

c#是区分大小写的。它区分大写字母和小写字母。在c#中,文件名可以不同于类名。这种语言使用分号作为语句的结束符。通过本课程了解更多关于编写自己的c#程序的知识

案例选择简介

选择案例,更常见于C#中的切换盒,因为它实际上在多个情况下切换。Switch语句包含一个值列表,如果变量与列表中的值匹配,则会选择该案例以执行。以下是切换盒的语法

Switch (variable){case constant-expression: statement(s);打破;Case常量表达式:语句(s);打破;/*不限制case语句的数量*/ default: statement(s);}

switch语句不限制case语句的数量。交换机变量的数据类型应与不同情况下的值相同。如果switch变量和case值匹配,那么将执行这种情况下的语句。它将继续执行,直到遇到break语句。break语句将程序控制转移到switch语句之后的行。break语句在switch语句中不是强制的。后者有一个可选的默认情况。默认大小写只能放在switch语句的末尾。当没有case为真时,可以使用默认语句执行所需语句。

示例1:一个c#开关案例程序示例

使用系统;class Program1 {static void Main() {int value = 4;switch(值){case 1: Console.WriteLine(1);打破;案例2:Console.WriteLine (2);打破;案例3:Console.WriteLine (3);打破;案例4:Console.WriteLine (4);打破; case 5: Console.WriteLine(5); break; default: Console.WriteLine(“None of the above”); break; } } }

在这个程序中,switch将计算整型变量value。由于它持有值4,程序控制将转移到情况4。Console.Writeline()方法将在输出屏幕上打印4。break语句将导致控制从switch中退出。在本课程中查看具有选择案例的更多现实世界示例。

例2:项目演示俱乐部多重案例

使用系统;class Program2 {static void Main(string[] args) {int switchValue = 3;switch (switchValue) {case 0: case 1: Console。WriteLine(“第一集”);打破;案例2:控制台。WriteLine(“第二套”);打破;情况9 - 6:控制台。WriteLine(“第三套”);打破; default: Console.WriteLine("Default case"); break; } } }

这里我们看到多个箱子标签可以堆叠在一起。如果您不使用break来终止您的case,代码将在执行过程中失败到下一个switch case。这意味着如果任何case没有break语句,它将不会退出switch case,相反,它将执行下一个case。在这里,Case 0和Case 1都返回到相同的print语句。病例9至6为第三组。只有在前面的情况不满足变量值时,才执行默认情况。

例3:程序演示字符选择案例

使用系统;类程序3 {静态void main(){char输入='d';字符串value1 = switchchar(输入1);console.writeline(value1);静态字符串SwitchChar(Char输入){Switch(输入){案例'一个“案例”A':{返回“Apple”;}案例'b'案例'b':{返回“香蕉”;}案例'c'case'c':{返回“樱桃”;}案例'd'案例'd':{返回“日期”;默认值:{返回“无效选项”;}}}}}

这里switch语句测试字符值是否相等。请注意,字符的小写和大写版本都堆叠在一起,每个字符必须在单引号中指定。学习更多关于在c#中使用字符变量的知识

示例4:演示在选择情况下使用字符串的程序代码

控制台。你喜欢咖啡吗?(是的/不/可能)”);string input = Console.ReadLine();switch(input.ToLower()) {case "yes": case "maybe": Console. tolower ();WriteLine(“很高兴听到这个”);打破;“不”:控制台。WriteLine(“也许你喜欢别的饮料!”);打破;默认值:控制台。WriteLine("I'm sorry, I didn't understand that!"); break; }

上面的程序对字符串值使用switch。如果字符串不匹配任何情况下,程序控制转移到默认标签。

例5:使用选择情况执行简单算术的程序

使用系统;namespace myprogram {class Program3 {static void Main(string[] args) {int yourchoice, number1, number2;浮动的结果;newlabel: Console.WriteLine(“\ n \ tMenu”);控制台。WriteLine("\nPress 1 for add ");控制台。WriteLine(“按2减”);控制台。WriteLine(“乘法请按3”); Console.WriteLine("Press 4 for Dividing"); Console.Write("\n\nInput the first number:\t"); number1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the second number:\t"); number2 = Convert.ToInt32(Console.ReadLine()); Console.Write("\nEnter your choice:\t"); yourchoice = Convert.ToInt32(Console.ReadLine()); switch (yourchoice) { case 1: result = number1 + number2; Console.WriteLine( result); break; case 2: result = number1 - number2; Console.WriteLine( result); break; case 3: result = number1 * number2; Console.WriteLine(result); break; case 4: result = (float)number1 / number2; Console.WriteLine( result); break; default: Console.WriteLine("\nInvalid choice Please try again."); goto newlabel; } Console.ReadLine(); } } }

这里首先显示一个菜单。然后提示用户选择所需的算术运算。switch语句相应地将程序控制转移到适当的情况下。在错误的情况下,选择goto关键字是用来转移控制到程序的开始。

希望本文能帮助您更好地理解c#中的一些案例。当然,选择案例只是c#语言的一部分。为了得到一个更全面的观点,你应该选修这门关于c#的综合课程

c#顶级课程

面向初学者的c#。net
里克•菲利普斯
4.4 (1269)
完成c# Unity Game Developer 3D
Ben Tristem, Rick Davidson(游戏开发者)电视团队
4.7 (27120)
畅销书
c# 7、c# 8和c# 9有什么新功能
Dmitri Nesteruk
4.6 (572)
c#中级:类,接口和面向对象
狂舞Hamedani
4.6 (23379)
畅销书
c#高级主题:为技术面试做准备
狂舞Hamedani
4.5 (15267)
完整的c#大师级
丹尼斯•Panjuta教程。Denis Panjuta的eu
4.5 (12,488)
c#和。net中的设计模式
Dmitri Nesteruk
4.4 (7200)
畅销书
裸体C#:初学者编码指南
Penny de byl,Penny @ holistic3d.com
4.4 (557)

更c#的课程

c#的学生也会学习

让你的团队。领导行业。

使用Udemy for Business订阅在线课程库和数字学习工具。

请求一个演示