# 字符串拼接

//需要输出{},则使用{{}}
public string fullName=>$"{firstName}-{lastName}" //属性
public void MyFullName()=>$"{firstName}-{lastName}" //方法

# using static

using static SharpGrammarInfo.SharpSixInfoTest.StaticClass
//方法内可以直接使用
Next();

# 异常筛选器

//把异常消息过滤
catch(Exception ex) when(ex.Message.Contains("111"))
{}

# nameof

获取方法的名称

//使用nameof当参数变化时会在引用的地方同步变化,避免硬编码
//nameof里面可以是:类名,方法名,参数名,属性名
public void NewMethod(int account1)
{
	if(account1 < 100)
	{
		throw new ArgumentException($"参数{nameof(account1)}值不能小于100");
	}
	else
	{
		//其他操作
	}
}

# 使用索引器初始化关联集合

直接设置默认值

Dictionary<int,string> message=new Dictionary<int,string>{
	{404,"Page not found"},
	{302,"Page not found"},
	{500,"Page not found"}
};
message.Add(405,"Page not found");

Dictionary<int,string> message=new Dictionary<int,string>{
	[404]="Page not found",
	[302]="Page not found",
	[500]="Page not found"
}
message.Add(405,"Page not found")

# out变量

//不需要声明,类型可以是var
int.TryParse(input,out var result);

//初始化的时候修改传入的值
public class BInfo{
	public BInfo(int a,out string b){
		b=a+1;
	}
}
public DInfo:BInfo{
	public DInfo(int c):base(c,out var d){
		console.WriteLine($"The value of 'd' is {d}");
	}
}

# 元组

(string aa,string bb) nameLetters=("a","b");
nameLetters.aa="111";//修改默认值
nameLetters.bb="222";//修改默认值

var nameLetters=(aa:"a",bb:"b");
nameLetters.aa="111";//修改默认值
nameLetters.bb="222";//修改默认值

//多个返回值
(int max,int min)=Range();
private static (int max,int min) Range(){
	return (123,234);
}

# 弃元

var (_,_,pop1_,pop2)=QueryData("aaa",150,200);
private static(string,double,int,int,int) QueryData(string name,int year1,int year2)
{}

# 模式

int a=123;
int b=456;
if(a is int c){
	b +=c
}
if(a is >1){
	b +=c
}
if(a is null){
	b +=c
}

# 本地方法

public static string loaclFunction(string name)
{
	return aaaa(name);
	//本地方法
	string aaaa(string name)
	{
	  return name;	
	}
	//静态本地方法
	static string aaaa(string name)
	{
	  return name;	
	}
}

# 默认值表达式

Func<string,bool> aaa=default;
string str=default;
int a=default;

# 实参位置可以变换

PrintOrder(orderNun:2,proName:"aaaa",price:110);

# private protected

private protected //访问限于当前类或当前程序集中派生类

# 默认接口方法

//调用
CustomClass myCustom=new CustomClass();
myCustom.Show();
myCustom.ShowInfo();

//类
public class CustomClass:ICustomClass{
	public void Show(){
		Console.WriteLine("this is show");
	}
}
//接口
public interface ICustomClass{
	public void Show();
	//默认接口方法
	public void ShowInfo(){
		Console.WriteLine("this is showinfo");
	}
}

# switch 新用法

//枚举
public static string MyWeek(WeekInfo week){
	week switch{
		WeekInfo.Monday=>"周一",
		WeekInfo.Tuesday=>"周二",
		_=>throw new NotImplementedException("枚举不存在"),//_表示不存在的情况
	}
}
//属性
public static int MyProperty(ProductInfo product){
	product switch{
		{Name:"产品名称"}=>product.Price*2,//属性Name是"产品名称"的时候
		{Price:100}=>product.Price*2,//属性Price是"100"的时候
		_=>throw new NotImplementedException(),
	}
}
//元组
public static string MyNames(string first,string second){
	(first,second) switch{
		("张","三")=>$"{first}-{second}",
		("李","四")=>$"{first}-{second}",
		(_,_)=>"不匹配",
	}
}

# 记录

//属性 init
public class Person{
	public string FirstName{get;init;}
	public string SecondName{get;init;}
}
var person=new Person(){
	FirstName="张",
	SecondName="三"
};
string firstName= person.FirstName;
string secondName= person.SecondName;
person.FirstName="李";//报错 不能赋值
person.SecondName="四";//报错 不能赋值

//类 record
public record Person{
	public string FirstName{get;init;}
	public string SecondName{get;init;}
	
	public Person(string first,string last){
		FirstName=first;
		SecondName=last;
	}
}

Person person=new Person("张","三");
string firstName= person.FirstName;
string secondName= person.SecondName;
person.FirstName="李";//报错 不能赋值
person.SecondName="四";//报错 不能赋值

//复制一份 Person 可以修改其值
var otherPerson=person with{FirstName="李"};
// record类也可以被继承
public record Teacher:Person{
	
}

# 简化对象声明

//以前
Person person=new Person("张","三");
//现在
Person person=new();
Person person=new(){"张","三"};

# 静态匿名函数

//以前
Func<int> func1=()=>1;
//现在
Func<int> func1=static ()=>1;

# 原生大小的数据类型

//nint、nuint、nfloat等 'n'表示native(原生)
//在32位的机器上编译需要4个字节,在64位的机器上编译需要8个字节
nint nativeInt=55;