二、C#笔记

2023-12-14 11:33:33

/// <summary>
/// 第三章:方法和作用域
/// </summary>
namespace Chapter3
{
? ? class Program
? ? {
? ? ? ? public static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? //3.1创建方法
? ? ? ? ? ? Console.WriteLine($"{addValues(1, 2)}");
? ? ? ? ? ? ///3.1.3 使用表达式主体方法
? ? ? ? ? ? void showresult(int answer) => Console.WriteLine($"the answer is {answer}");

? ? ? ? ? ? //3.2调用方法
? ? ? ? ? ? showresult(100);

? ? ? ? ? ? //3.3使用作用域
? ? ? ? ? ? ///3.3.1定义局部作用域
? ? ? ? ? ? ///3.3.2定义类作用域
? ? ? ? ? ? ///3.3.3重载方法
? ? ? ? ? ? ///3.3.4编写方法

? ? ? ? ? ? //3.4 使用visual studio调试器来调试方法
? ? ? ? ? ? ///3.4.1重构代码
? ? ? ? ? ? /* 在应用程式的多个位置写相同的代码,选定并右击输入的代码块,选择快速操作和重构,再单击提取方法*/

? ? ? ? ? ? //3.5使用可选参数和具名参数
? ? ? ? ? ? ///3.5.1定义可选参数
? ? ? ? ? ? optMethod(99, 123.45, "world");//全部三个参数都提供了实参
? ? ? ? ? ? optMethod(100, 54.321);//只为前两个参数提供了实参
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?///3.5.2传递具名参数
? ? ? ? ? ? optMethod(first: 99, third: "world");
? ? ? ? ? ? ///3.5.3消除可选参数和具名参数的歧义
? ? ? ? ? ? ///提供具名参数,编译器会将参数名和方法声明中指定的参数名比较,并选择参数名称匹配的方法。
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 3.1.1 声明方法
? ? ? ? /// </summary>
? ? ? ? /// <param name="left"></param>
? ? ? ? /// <param name="right"></param>
? ? ? ? /// <returns></returns>
? ? ? ? static int addValues(int left,int right)
? ? ? ? {
? ? ? ? ? ? //3.1.2 从方法返回数据
? ? ? ? ? ? return left + right;
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 3.2.2 从方法返回多个值(元祖)
? ? ? ? /// </summary>
? ? ? ? /// <param name="left"></param>
? ? ? ? /// <param name="right"></param>
? ? ? ? /// <returns></returns>
? ? ? ? //static (int,int) addValues2(int left, int right)
? ? ? ? // ? ?{
? ? ? ? // ? ? ? ?return (left,right);
? ? ? ? // ? ?}

? ? ? ? ///3.4.2嵌套方法
? ? ? ? static long CalculateFactorial(string input)
? ? ? ? {
? ? ? ? ? ? int inputValue = int.Parse(input);
? ? ? ? ? ? long factorial(int dataValue)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (dataValue == 1)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return dataValue * factorial(dataValue - 1);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? long factoricalValue = factorial(inputValue);
? ? ? ? ? ? return factoricalValue;
? ? ? ? }

? ? ? ? /// <summary>
? ? ? ? /// 3.5.1
? ? ? ? /// </summary>
? ? ? ? /// <param name="first"></param>
? ? ? ? /// <param name="second"></param>
? ? ? ? /// <param name="third"></param>
? ? ? ? static void optMethod(int first, double second=0.0,string third = "hello")
? ? ? ? {

? ? ? ? }


? ? }
}
?

/// <summary>
/// 第四章:使用判断语句
/// </summary>
namespace Chapter4
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? One();//4.1声明布尔变量
? ? ? ? ? ? Two();//4.2使用布尔操作符
? ? ? ? ? ? Three();//4.3使用if语句判断
? ? ? ? ? ? Four(0);//4.4使用switch语句做判断
? ? ? ? ? ? Five(8);//4.5为switch表达式使用模式匹配
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }

? ? ? ? private static void Five(int v)
? ? ? ? {
? ? ? ? ? ? switch (v)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? ? ? v += 1;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? v += 1;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? //int a = 0;
? ? ? ? ? ? //a = v switch
? ? ? ? ? ? //{
? ? ? ? ? ? // ? ?0 => 1,
? ? ? ? ? ? // ? ?1 =>1,
? ? ? ? ? ? // ? ?_ =>1
? ? ? ? ? ? //};

? ? ? ? }

? ? ? ? private static void Four(int num)
? ? ? ? {
? ? ? ? ? ? //4.4.1理解switch语句的语法
? ? ? ? ? ? switch (num)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? ? ? Console.WriteLine("num=0");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? Console.WriteLine("num!=0");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? //4.4.2遵守switch语句的规则
? ? ? ? ? ? /*
? ? ? ? ? ? ?* 1.switch语句的控制表达式只能是某个整形或string。其他的类型只能用if
? ? ? ? ? ? ?* 2.case标签必须是常量的表达式,如42等等。要是想在运行时计算case标签的值,就只能用if语句
? ? ? ? ? ? ?* 3.case标签必须唯一,不允许两个case标签具有相同的值
? ? ? ? ? ? ?* 4.可以续写多个case标签(中间不间插额外的语句)
? ? ? ? ? ? ?*/
? ? ? ? ? ? switch (num)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? case 2: //允许直通
? ? ? ? ? ? ? ? ? ? Console.WriteLine("num=2"); //1和2都执行相同的代码
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? //case 3:
? ? ? ? ? ? ? ? // ? ?Console.WriteLine("num=3");//出错标签之间有额外的代码
? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? Console.WriteLine("num=4");
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }

? ? ? ? }

? ? ? ? static void One()
? ? ? ? {
? ? ? ? ? ? bool areYouReady;
? ? ? ? ? ? areYouReady = true;
? ? ? ? ? ? Console.WriteLine(areYouReady);
? ? ? ? }

? ? ? ? static void Two()
? ? ? ? {
? ? ? ? ? ? //4.2.1理解相等和关系操作符(==,!=,<,>,<=,>=)
? ? ? ? ? ? //4.2.2理解条件逻辑操作符(&&,||)
? ? ? ? ? ? //4.2.3短路求值(针对||)
? ? ? ? ? ? //4.2.4操作符的优先级和结合性总结
? ? ? ? ? ? //1.<,>,<=,>=
? ? ? ? ? ? //2.==,!=
? ? ? ? ? ? //3.&&,||
? ? ? ? ? ? //4.=(赋值)
? ? ? ? ? ? //4.2.5模式匹配
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? bool valiPercentage;
? ? ? ? ? ? ? ? //int percent = int.Parse(Console.ReadLine());
? ? ? ? ? ? ? ? //valiPercentage = (percent is <0 and <=100); ?C#8.0
? ? ? ? ? ? }
? ? ? ? ? ? catch
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("请重新输入!");
? ? ? ? ? ? ? ? Console.ReadLine();
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? static void Three()
? ? ? ? {
? ? ? ? ? ? int percent = int.Parse(Console.ReadLine());
? ? ? ? ? ? //4.3.1理解if语句的语法
? ? ? ? ? ? if (percent>=100)
? ? ? ? ? ? ? ? Console.WriteLine("hh!");
? ? ? ? ? ? else
? ? ? ? ? ? ? ? Console.WriteLine("heng!");
? ? ? ? ? ? //4.3.2使用代码块分组语句(用括号)
? ? ? ? ? ? //4.3.3嵌套if语句
? ? ? ? ? ? if (percent > 100)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("aa!");
? ? ? ? ? ? }else if (percent > 50)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("bb!");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("cc!");
? ? ? ? ? ? }


? ? ? ? }
? ? }
}
?

文章来源:https://blog.csdn.net/H215919719/article/details/134856919
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。