一、控制台IO
-
Write()与WriteLine()区别
Write()为直接输出;
WriteLine()为输出后光标换行 -
Read(),ReadKey()与ReadLine()
Read():读取用户键盘操作,每次读取用户的第一次键盘操作,输出为键入的ASCII码
ReadLine():无长度限制,读取用户输入的全部数据直至按下回车
ReadKey():可以读取一些特殊字符,例如读取回车
ConsoleKeyInfo a = Console.Readkey();
Console.writeLine(a.key);
- Clear()
清除命令行内容
二、变量(Variable)与数据类型(Data Type)
-
基本数据类型
-
字符串常用操作
字符串拼接,使用“+”
onsole.WriteLine("Hello,“+” ALEX!");
字符串格式化处理
string name = "John";
int age = 18;
Console.WriteLine("My name is {0}, I am {1} years old.", name, age);
string message = "My name is {0}, I am {1} years old.";
string output = string.Format(message, name, age);
Console.WriteLine(output);
字符串内嵌
string message2 = $"My name is {name}, I am {age} years old.";
Console.WriteLine(message2);
愿意字符串
string message3 = @"My name is {name},
I am {age} years old.";
Console.WriteLine(message3);
三、代码决策
if(){
}else if{
}else{
}
switch(){
case: ; break;
case: ; break;
default: ; break;
}
三元运算
condition ? consequent1 : consequent2
四、循环
for loop
while loop
do while loop
五、方法
方法签名
<访问修饰符> <声明修饰符> <返回类型> <方法名称> (参数列表) {
method body
}
//访问修饰符:
public, private, protected,
private protected, internal, protected internal//内部方法,同一个程序集中的所有类都可以访问
//声明修饰符
static//表示静态类型
abstract//表示抽象类型
virtual//允许派生类重写的虚函数
override//允许方法继承后重写
new//可以隐藏基类成员
sealed//表示不能被继承
partial//允许在同一个程序集分散定义
extern//用于声明外部实现的extern
方法表达式
public static int FindMax(int num1, int num2)
{
int result;
if (num1 > num2)
{
result = num1;
}
else
{
result = num2;
}
return result;
}
public static int Findmax(int num1, int num2) =>
num1 > num2 ? num1 : num2;
六、传参
值传参
参数默认传递方式
为每个值参数创建一个新的存储位置
当形参的值发生改变时,不会影响实参的值,实参数据安全
函数传参
static void swap(ref int x, ref int y)//reference,参数的引用
{
int temp;
temp = x;
x = y;
y = temp;
}
static void Main(string[] args)
{
int a = 100;
int b - 500;
swap(ref a, ref b);
}
输出传参
static void swap(ref int x, ref int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
static void getValue(out int x)
{
x = 5;
}
static void Main(string[] args)
{
int a = 100;
int b - 500;
swap(ref a, ref b);
getValue(out a);//直接将方法内部的数据传出去
}