Using system & Console
& hello world
system是c#程序最基本的模块,这个模块可以提供各种各样已经提前定义好的功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 using System;namespace HelloWorld { class Program { static void Main (string [] args ) { Console.WriteLine("Hello World!" ); } } }
Console工具类中常见方法
Write(): 括号内参数显示在命令行中
WriteLine(): 括号内参数显示在命令行中,显示完成后换行
Read():
只读取第一次按下键盘的按钮(包含字符,ascii代码等本身所有信息)
ReadKey(): 只读取第一次按下键盘的按钮(ascii代码)
ReadLine(): 读取用户输入,直到按下回车
Clear(): 命令行所有内容清除
列表,index索引,范围
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 string [] words = new string []{ "The" , "quick" , "brown" , "fox" , "jumped" , "over" , "the" , "lazy" , "dog" }; Console.WriteLine(words[1 ]); Console.WriteLine(words[words.Length-1 ]); Console.WriteLine(words[^1 ]); var list = words[0. .3 ];
其中C#有两个类型专门用来储存index索引与范围:Index
和Range
在C#
7出现Span
它可以表示另一个数据结构里连续相邻的一串数据,并且它是内存安全的。C#
8 里面出现Range
和Index
,
还可以组合使用Range
和Index
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 string [] words = new string []{ "The" , "quick" , "brown" , "fox" , "jumped" , "over" , "the" , "lazy" , "dog" }; Range range = 1. .4 ; Index index = ^1 ; Range complexRange = 1. .^1 ; var newList1 = words[range];var newList2 = words[index];var newList3 = words[complexRange];
逻辑判断(基本与Java一致,不赘述)
if - else
1 2 3 4 5 6 if (weather == good){ goSwimming(); } else { notGoSwimming(); }
switch
1 2 3 4 5 6 7 8 9 10 11 12 13 switch (expression){ case constant-expression : statement(s); break ; case constant-expression : statement(s); break ; default : statement(s); break ; }
?:操作符
1 condition ? consequent : alternative
循环(基本与Java一致,不赘述)
for循环
1 2 3 for (int counter = 0 ; counter < 10 ; counter = counter + 1 ) { Console.WriteLine("counter: " + counter); }
while循环
1 2 3 4 int couter = 0 while (conter<5 ){ counter = counter + 1 ; }
do while循环
1 2 3 4 int couter = 0 do { counter++; } while (counter < 5 )
方法(Method)
方法定义(与Java有几处略有不同,不同之处红色字体标出)
1 2 3 4 5 6 7 8 9 <Access Specifier> <Modifier> <Return Type> <Method Name> (Parameter List){ Method Body }
访问修饰符(Access Specifier)
定义了方法的访问属性,我们可以选择这个列表中的任意一个来描述方法的可见性 public:
公有方法,可以被外部调用 private:
私有,方法会被隐藏起来,其他class是不能调用的 Protected:
只能在它的类本身或者它的派生类中访问 Internal:
内部方法,同一个程序集(同名.cs文件)中的所有类都可以访问
声明修饰符 (modifier)
C#中共有8个声明修饰符:
静态类型(static)
抽象类型(abstract)
派生类重写的虚函数(virtual)
允许方法继承后重写的override(与Java写的位置不同)
隐藏基类成员继承的new
表示不能被继承的sealed
允许在同一个程序集分散定义的partial
用于声明外部实现的Extern
方法表达式
除了普通的、使用花括号的方法定义方式,他还给我们提供了一个更加简单的语法结构,这种语法结构就是箭头表达式 方法的签名保持不变,变化的是方法的主题部分
1 public static int FindMax (int num1, int num2 ) => num1 > num2 ? Num1 : num2
值传参与引用传参
值传参不必多说 1 2 3 4 5 6 7 8 public void swap (int x, int y ){ int temp; temp = x; x = y; y = temp; }
按引用传递参数
swap方法的两个参数前面加上关键词ref,实现引用传参 1 2 3 4 5 6 7 8 public void swap (ref int x, ref int y ){ int temp; temp = x; x = y; y = temp; }
按输出传递参数
1 2 3 4 5 6 7 public void getValue (out int x ){ int temp = 5 ; x = temp; } getValue(a);
这时候如果我们在调用getValue方法的同时,也加上out关键词,那么这个时候方法中x的数据就会突破限制,直接向参数a输出