《C# 6.0 本质论》 阅读笔记

阅读笔记不是讲述这本书的内容,只是提取了其中一部分我认为比较重要或者尚未掌握的知识,因此若是有错误或者模糊之处,请指正,谢谢!数组

对于C# 6.0才有的新语法基本都有标记,开发的时候要注意使用,以避免形成不兼容现象。多线程

 

一。第一章
1.控制台中可使用,其会在每一次输入的时候获取键盘输入的值。能够用Console.ReadKey().KeyChar获取输入的字符。函数

void UseReadKey() { var a = Console.ReadKey().KeyChar; Console.WriteLine(a); }

2.C#6.0新语法:$"this is {变量名}" 能够在大括号中直接输入变量名调用,相似于string.Format(),更加简洁。this

void UseNewFormat() { string FirstName = "First"; string LastName = "Last"; Console.WriteLine($"The Name is :{FirstName} {LastName}"); }

3.静态引用 using Static System.Console; 能够将Console.WriteLine();缩写成WriteLine(); 仅能对静态函数缩写。spa

using static System.Console; void UseStaticUsing() { string Str = Read(); WriteLine(Str); }

 

二。第二章
1.小数位过多时候,直接转换为字符串会丢失精度,使用 string.Format("{0:R}",a);就不会丢失精度。线程

void UsingFormatR() { double Str=3.15489764531879874; Console.WriteLine(Str);//会丢失一部分 显示3.154897645318
   Console.WriteLine("{0:R}",Str);//不会丢失 显示3.1548976453187989 为何会比赋值数值少,由于 }

2.可空修饰符 值类型+? 默认值为nullcode

void UseNullable() { double? Db = null; int? It = null; }

3.尝试转换 值类型特有 只在能够转换的时候进行转换。 orm

string a = "456789"; string b = "abce456"; int inta=0; int intb=0; var boola = int.TryParse(a,out inta);//返回true,inta=456789
 var boolb= int.TryParse(b,out intb);//返回false,intb=0

 

4.溢出检测 checked{} 当块内的数据溢出的时候会抛出错误。对象

checked { long b = 894156132165798; int a = Convert.ToInt16(b); }

 

5.返回值 default(T) 能够返回任何类型的默认值。blog

return default(int?); return default(int);

 

6.二维数组 int[,] a=new int[3,4]; Console.WriteLine(a[1,1]);
   交错数组 int[][] a={new int[] {1,2},new int[] {3,4}};Console.WriteLine(a[1][1]);

7.数组扩充 Array.Resize(ref a,100); 将原有数组a中的内容复制到新数组中,而且新数组的长度为100,而后将原有数组a指向新数组。

int[] a = new int[10];//数组a的长度为10
            Array.Resize(ref a, 100);//数组a的长度为100



三。第三章
1.数值型数据两个特殊值:Nan Not a Number(非数值) ,Infinty(无穷)

2.在多线程中使用i++,i--等自增自减,因为该操做不是原子性操做,可能形成竞争资源。为了防止该现象出现,可使用System.Threading.Interlocked.Increment(ref i);自增
   或者System.Threading.Interlocked.Decrement(ref i);自减

int i = 10; System.Threading.Interlocked.Increment(ref i);//i=11
            System.Threading.Interlocked.Decrement(ref i);//i=10

3. ??操做符 二元操做符号,当左边的值为null时候取右边的值。 左边的值必须是能够为null的,包括引用类型和可空类型。

int? a = null; int b=  a ?? 18;

4.C#6.0新语法 ?. 判断实例是否为null,若是返回值为值类型会返回值的可空类型。例如new Person() ?.age的值是0

5.位运算【!!!!未学明白!!!!】 ^(异或) ,&(同或),|(与非),~(按位取反), 掩码,位映射

6.预处理

四。第四章
1.命名空间使用别名

using CountDownTimer=System.Timers.Timer;

2.可变参数 :

private static void xianshi(params string[] str) { foreach (var item in str) { Console.WriteLine(item); } }

参数带params表示该函数是可变参数,可变参数必须是一位数组,能够接受该类型的值和一维数组的值。且可变参数必须是参数列表最后一个参数(仅能够一个)。
3.可选参数:在参数中能够给参数赋初始值,当没有输入该参数的时候,将会使用默认值。

private static void GetList(string whereStr = "", string OrderStr = "", string Go = "") { Console.WriteLine(whereStr + OrderStr + Go); }

3.指定参数名调用

五。第五章 类
1。资源回收:GC会自动回收托管资源。 隐式肯定性资源清理 使用终结器 ;显式肯定性资源 使用using(){};
2。自动属性也能够赋默认值。 public string Name{get;set;}="NoName";
3.C#6.0 :nameof(<变量>) 取变量名变成字符串,能够防止当变量名改变后,忘记修改对应的字符串。 语法糖。
4.C#6.0 :支持只读属性的自动实现(赋默认值) 5.对象初始化器 :在已有Person类的状况下: Person Ps=new Person(){Name="Liu",Age="11"}; 6.构造器链:构造函数1:public Employee(string firstName ,string lastName) {this.FirstName=firstName;this.LastName=lastName} 构造函数2:public Employee(int id,string firstName ,string lastName):this(firstName,lastName){this.ID=id}; 构造函数2使用了构造函数1的方法,造成构造链。 7.静态构造函数:在类被首次访问的时候执行。 8.静态属性:静态字段可使用属性实现。 9.静态类:编译器会自动标记为abstract和saled ,因此不能够继承。 静态类中实例方法是没有意义的。 10.分部类:partial class Person{} ;分部类只容许在同一程序集中实现。分部类须要声明方法才能够调用,而且分部方法以前要加partial,分部方法只能返回void,且不容许使用out,可使用ref。

相关文章
相关标签/搜索