C#6相关语法
C#里=>的用法,属性和方法都可用
Console.WriteLine(A.x);// 0
A a = new A();
Console.WriteLine(a.X1);// 0
Console.WriteLine(a.X2);// 0
A.x = 5;
Console.WriteLine(a.X1);// 5
Console.WriteLine(a.X2);// 0
A.x = 10;
Console.WriteLine(a.X1);// 10
Console.WriteLine(a.X2);// 0
public class A
{
public static int x;//默认初始化为0
public int X1 => x;
public int X2 = x;
}
--------------------------------------------------------------------------------------------------------------
public int MaxHealth1 => x ? y:z;
等同于下面
public int MaxHealth1
{
get
{
return x ? y:z;
}
}
--------------------------------------------------------------------------------------------------------------
字符串内插
string FirstName=“1111";
string LastName="333333";
string.Format("{0}-{1}", this.FirstName, this.LastName)
等同于
string FullName => $"{FirstName}-{LastName}"
--------------------------------------------------------------------------------------------------------------
? 和 ?? 区别
? 表示为可空类型修饰符,定义数据类型可为空,目的是用于对 int,double,bool 等无法直接赋值为 null 的类型进行 null 的赋值
int? a1 = null;
double? a2 = null;
bool? a3 = null;
??空合并运算符
空合并运算符,s = a??b, 当a为null的时候, b赋为s, 当a不等于null的时候将a赋为s。注意空合并符为有合并符,即是从右往左进行合并,例如 a??b??c 相当于 a??(b??c)
--------------------------------------------------------------------------------------------------------------
使用索引器初始化关联集合
Dictionary<int, string> webErrors = new Dictionary<int, string>
{
[404] = "Page not Found",
[302] = "Page moved, but left a forwarding address.",
[500] = "The web server can't come out to play today."
};
等同于
Dictionary<int, string> messages = new Dictionary<int, string>
{
{ 404, "Page not Found"},
{ 302, "Page moved, but left a forwarding address."},
{ 500, "The web server can't come out to play today."}
};
等同于
Dictionary<int, string> messages = new Dictionary<int, string>
messages.Add(405,"朝夕教育");
messages.Remove(405, out string obj);
--------------------------------------------------------------------------------------------------------------
C#7相关语法
元组
(string Alpha, string Beta) namedLetters = ("a", "b");
namedLetters.Alpha = "aa";
namedLetters.Beta = "bb";
Console.WriteLine($"{namedLetters.Alpha}, {namedLetters.Beta}");
var alphabetStart = (Alpha: "a", Beta: "b");
alphabetStart.Beta = "B+B";
alphabetStart.Alpha = "A+A";
Console.WriteLine($"{alphabetStart.Alpha}, {alphabetStart.Beta}");
(int max, int min) = Range(); //返回方法也支持元祖模式
Console.WriteLine(max);
Console.WriteLine(min);
private static (int max, int min) Range()
{
return (123, 234);
}
-------------------------------
var p = new Point(12, 13);
Console.WriteLine(p.X);
Console.WriteLine(p.Y);
p.Deconstruct(out double xx, out double yy);
Console.WriteLine(xx);
Console.WriteLine(yy);
public class Point
{
public Point(double x, double y)
=> (X, Y) = (x, y);
public double X { get; }
public double Y { get; }
public void Deconstruct(out double x, out double y) =>
(x, y) = (X, Y);
}
--------------------------------------------------------------------------------------------------------------
弃元
主要是 _ 这符号,意思不用获取返回值
var (_, _, _, pop1, _, pop2) = QueryCityDataForYears("New York City", 1960, 2010);
(string, double, int, int, int, int) result = QueryCityDataForYears("New York City", 1960, 2010);
--------------------------------------------------------------------------------------------------------------
模式
int input = 123;
int sum = 234;
if (input is int count)
sum += count;
----------------
IEnumerable<object> enumerablelist = new List<object>()
{
0,
new List<int>(){ 0,1,2,3,4,5,6 },
100,
null
};
int iResult = SumPositiveNumbers(enumerablelist);
/// <summary>
/// 模式
/// </summary>
/// <param name="sequence"></param>
/// <returns></returns>
public static int SumPositiveNumbers(IEnumerable<object> sequence)
{
int sum = 0;
foreach (var i in sequence)
{
switch (i)
{
case 0:
break;
case IEnumerable<int> childSequence:
{
foreach (var item in childSequence)
sum += (item > 0) ? item : 0;
break;
}
case int n when n > 0:
sum += n;
break;
case null:
throw new NullReferenceException("Null found in sequence");
default:
throw new InvalidOperationException("Unrecognized type");
}
}
return sum;
}
--------------------------------------------------------------------------------------------------------------
本地方法
LocalFunction("AAAAAAAAAAAAAAAAAAAAAA");
public static string LocalFunction(string name)
{
return ZhxiToString(name);
string ZhxiToString(string name)
{
return name;
}
}
--------------------------------------------------------------------------------------------------------------
默认文本表达式
Func<string, bool> whereClause = default(Func<string, bool>);
Func<string, bool> whereClause1 = default;
string str = default;
int i = default;
default赋值为null,如果int型为0
--------------------------------------------------------------------------------------------------------------
访问修饰符---新复合访问修饰符
// 访问修饰符是关键字,用于指定成员或类型已声明的可访问性。 本部分介绍四个访问修饰符:
//可使用访问修饰符指定以下六个可访问性级别:
//public:访问不受限制。
//protected:访问限于包含类或派生自包含类的类型。
//internal:访问限于当前程序集。
//protected internal:访问限于当前程序集或派生自包含类的类型。
//private:访问限于包含类。
//C#7特有
//private protected:访问限于包含类或当前程序集中派生自包含类的类型。
--------------------------------------------------------------------------------------------------------------
通用的异步返回类型
ValueTask 还没搞懂什么意思