1、switch case :
int a = ;
switch (a)
{
case :
Console.WriteLine("一");
break;
case :
case :
Console.WriteLine("二或三");
break;
}
2、for循环 :
for(<初始条件>; <判断条件>; <更新条件>)
{
<代码块>
}
double d;
for (string x = Console.ReadLine(); double.TryParse(x, out d); x = Console.ReadLine())
{
Console.WriteLine("您输入的数字是:{0}\n", d);
}
3、函数调用
调用方法是可以标注参数对应的值,标注同时,可以不按方法参数表顺序传参:
static int Add(int a, int b)
{
return a + b;
}
static void Main(string[] args)
{
int c = Add(, );
int d = Add(b: , a: );
}
4、引用类型
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
} class Program
{
static void Main(string[] args)
{
// 创建 ps1, 并实例化ps1
Person ps1 = new Person { Name = "Tim", Age = };
// 创建ps2用ps1给ps2赋值
Person ps2 = ps1;
Console.WriteLine("ps1被修改前ps2的值:\nps2.Name = {0}, ps2.Age = {1}", ps2.Name, ps2.Age); // 修改ps1的值
ps1.Name = "Jack";
ps1.Age = ;
// 修改ps1后,ps2的值
Console.WriteLine("修改ps1后,ps2的值:\nps2.Name = {0}, ps2.Age = {1}", ps2.Name, ps2.Age);
}
}
输出:
ps1被修改前ps2的值:
ps2.Name = Tim, ps2.Age = 22
修改ps1后,ps2的值:
ps2.Name = Jack, ps2.Age = 18
5、值类型
public struct Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
// 创建 ps1, 并实例化ps1
Person ps1 = new Person { Name = "Tim", Age = };
// 创建ps2用ps1给ps2赋值
Person ps2 = ps1;
Console.WriteLine("ps1被修改前ps2的值:\nps2.Name = {0}, ps2.Age = {1}", ps2.Name, ps2.Age); // 修改ps1的值
ps1.Name = "Jack";
ps1.Age = ;
// 修改ps1后,ps2的值
Console.WriteLine("修改ps1后,ps2的值:\nps2.Name = {0}, ps2.Age = {1}", ps2.Name, ps2.Age);
}
}
输出:
ps1被修改前ps2的值:
ps2.Name = Tim, ps2.Age = 22
修改ps1后,ps2的值:
ps2.Name = Tim, ps2.Age = 22
摘自《C# 6.0 学习笔记》