string是不可变的引用类型,改变变量值时,并不会替换原来的值,
在堆上会为新值分配一个新对象。
@允许在字符串字面量中包含换行符
$允许把花括号放在包含一个变量甚至代码表达式的字符串中,
变量或代码表达式的结果放在字符串中花括号所在的位置
string s1="a string";
WriteLine($"s1 is {s1}");
可空类型:HasValue判断 是否为空,Value返回底层的值。
int? x3=1;
int x5=x3.HasValue? x3.Value:-1;
合并操作符 int x6=x3 ?? -1;
枚举解析:
ConsoleColor red;
if(Enum.TryParse<ConsoleColor>("Red",out red))
{
Console.WriteLine($"{red}");
}
扩展方法在不修改类的同时给类增加功能,扩展方法也可用于扩展接口。
public static class stringExtension
{
public static int getWordCount(this string s) =>
s.Split().Length;
}
string f = "324";
f.getWordCount();