@laofang
2016-06-12T15:53:59.000000Z
字数 8199
阅读 1332
.Net
挑选了一些看书是看起来比较有意思的东西记录一下. 重新看发现自己的笔记有点乱, 没头没尾的. 但是, 不得不说这真是本好书. 很好看懂.
看这本书,不求都懂, 对C#教之前能有一个大局观上的认识提升.
书看起来挺厚, 但是通俗易懂.
适合作为入门书籍
不适合作为工具书
string[] args)Pascal大小写风格camel 风格大小写(驼峰命名法)CIL: 公共中间语言(Common Intermediate Language)JIT: 即时编译(just-in-time compilatiion)CLS: 公共语言规范(Common Language Specification)CLI: 公共语言基础结构规范(Common Language Infrastructure)BCL: 基类库(Base Class Library)字面值定义为decimal: 数值后面加m或MSystem.Console.WriteLine("0x{0:X}",42)@符号, 致命转移序列不被处理, 直接原样输出属性: 具有getter方法和setter方法的特殊方法引用类型,指针类型,可空类型double.TryParse(input, out output)default关键字可以获取数据类型的默认值: int count = default(int);string.Format()尤其是那些不好理解的代码块, 用有意义的方法名清晰地定义代码的行为,与简单的为代码块添加一个注释效果要好得多using不会导入 任何嵌套命名空间中的类型. 必须现实导入using不允许使用通配符. 所有命名空间必须显示引入参数默认是值传递的, 也就是说参数会复制一份到方法中而原来的值不会改变
- 引用参数ref: 调用者应该对参数进行初始化
- 输出参数out: 调用者不需要初始化, 方法体内对参数赋值后输出给调用者
二者在实现功能上完全一致
参数数组: 声明param关键字. 参数数组不一定是方法中唯一的参数, 但一定要是最后一个, 每个方法最多只有一个参数数组
例:将传入的参数构造成合法路径格式
static string Combine(params string[] paths){string result = string.Empty;foreach(string path in paths){result = System.IO.Path.Combine(result,path);}}
static void Main(){DisplayGreeting(firstName:"Inigo", lastName:"Montoya");}public void DispaltGreeting(string firstName,string middleName = default(string),string lastName = default(string)){//............}//便利: 如果一个方法许多参数都有默认值, 这种做法极大方便编程//代价: 牺牲接口灵活性: 方法参数名改变会带来编译错误
常规catch : try{}catch{}实例字段: 在类的级别上声明的变量, 用于存储与对象关联的数据自动实现属性: public string Name{get;set;};
class User{//字段private string _Name;//属性public string Name{get{return _Name;}set{_Name = value;}}//自动实现的属性public string age{get;set;}}//调用Class Program{static void main(){User user = new User();User.Name = "Zhangsan";Console.WriteLine(User.Name);}}/**规范考虑为支持字段和属性使用相同的大小写风格考虑属性和它的类型通明避免使用camel风格命名字段不要声明public或者protected的字段, 而应该声明private然后通过属性公开有限使用自动实现的属性而不是字段如果没有额外的实现逻辑, 要优先使用自动实现的属性而不是编写属性的完整版本**/
构造器: 即构造方法, 没有返回类型 && 方法名与类名相同
new操作符的实现细节
- new操作符从内存管理器中获取"空白"内存,
- 调用指定的构造器, 对"空白"内存的引用作为隐式的
this参数传给构造器- 构造器链的剩余部分开始执行, 在构造器之间传递引用,这些构造器都没有返回类型
- 构造器链上的执行结束后, new操作符返回内存引用. 现在, 该引用指向内存处于初始化好的形式
静态构造器: 首次访问类是自动调用, 不能显示调用, 不允许任何参数
class Employee{static Employee(){Random randomGenerator = new Random();NextId = randomGenerator.Next(101,999);}public static int NextId = 42;//执行结果: 先执行声明语句:NextId = 42, 随后访问构造器: NextId 等于一个随机值//另外: 普通的构造器(实例构造器)执行的结果也是这样}
//File: a.cspartial class Program{}//File: b.cspartial class Program{}
public class program{public static void Main(){//假设Contact是PdaItem的子类Contact contact = new Contact();//隐式转型PdaItem item = contact;//显示转型,而且显示类型是必须的contact = (Contact)item;}}
public class PdaItem{//不添加 virtual 关键字会报错public virtual string Name{get;set;}}public class Contact:PdaItem{//不添加override关键字会报严重警告public override string Name{get{ return FirstName + " " + LastName; }set{string[] names = value.Split(' ');FirstName = names[0];LastName = names[1];}}public string FirstName{get;set;}public string LastName{get;set;}}//"运行时"遇到虚方法时, 会调用虚成员派生的最远的重写即 item.Name = "..."会执行Contact类中的方法--->虚方法不应该包含关键代码
struct Angle{public int Degress { get;}public int Minutes { get; }public int Seconds { get; }//结构体只能声明有参构造器, 不能定义无参构造器public Angle(int degress, int minutes, int seconds){Degress = degress;Minutes = minutes;Seconds = seconds;}//返回一个新的值类型public Angle Move(int degress, int minutes, int seconds){return new Angle(Degress + degress,Minutes + minutes,Seconds + seconds);}}
装箱过程:
- 在堆上分配内存,用于存放值类型的数据以及少许的额外开销
- 发生一次内存复制, 将当前存储位置的值类型数据赋值到堆上
- 对堆上存储位置进行引用
class DisplayFibonaci{static void Mian(){int totalCount;System.Collection.ArrayList list = new System.Collection.ArrayList();Console.Write("Enter a numner between 2 and 1000:");totalCount = int.Parse(Console.ReadLine());list.Add((double)0);//装箱, Add方法的参数是objectlist.Add((double)1);//装箱for(int count = 2; count < totalCount; count ++){//拆箱(两次): 加法运算需要值类型//装箱: Add方法参数类型为引用类型list.Add(((double)list[count-1] + (double)list[count-2]));}//拆箱: list内容为object, 赋值给double类型的count需要拆箱foreach(double count in list){//装箱: Console.Write(string format, object args)Console.Write("{0}\t",count);}}}
enum ConnectionSate : short//修改默认基础类型{DisConnected, //0Connecting = 10, //10 --- 修改默认值Connected, //11Joined = conected, //11Disconnecting //12}
public struct Coordinate{public Coordinate(Longitude longitude, Latitude latitude){_Longitude = longitude;_Latitude = latitude;}public Longitude Longitude{get{return _Longitude;}}privete readonly Longitude _Longitude;public Latitude Latitude{get{return _Latitude;}}privete readonly Latitude _Latitude;//重写ToString方法public override string Tostring(){return string.Formate("{0} {1}",Longitude, Latitude);}}
public static [DataType] operator [Ppertor]
public static bool operator ==(){...}public static object operator + (){...}例: 检查坐标是否相等public static bool operator ==(Coordinate leftHandSide, Coordinate rightHandSide){//Check if leftHandSide is also null//operator == would be recursive--使用 == 来检查null会造成死递归if(ReferenceEquals(leftHandSide,null)){return ReferenceEquals(rightHandSide,null);}return leftHandSide.Equals(rightHandSide));}
public staic bool operator true(IsValid item){...}public tatic bool operator false(IsValid item){...}
//在Latitute和double之间提供隐式转换public struct Latitude{//------转换准备privete readonly double _DecimalDegress;public Latitude(double dicimalDegress){_DecimalDegress = Normalizi(decimalDegress);}public double DecimalDegress{get{return _DecimalDegress;}}//-------转换public static implicit operator double(Latitude latitude){return latitude.DecimalDegress;}public static implicit operator Latitude(double degress){return new Latitude(degress);}}//
win: csc.exe /r:cooridnates.dll Program.csmono: msc.exe /r:cooridnates.dll Program.cs*
/doc:sss.xml sss.cs --- /doc:目标文件名 原文件名
引用的对象所占用的内存
//利用终结器清理资源:语法 ~类名class FileHelper{//构造器public FileHelper(){...}//终结器:--不能带参数 终结器不能显示调用, 由运行时在恰当的时间运行//对象最后一次使用之后并在程序正常关闭之前~FileHelper(){....}}
//接口interface A<T>//类class B<T>calss C<T> : A<T>//接口约束//---------规定I必须实现IComparable接口class BinaryTree<T> where T: System.IComparable<T>//---规定value必须为自定义实体类型class myDic<TKty,TValue> where TValue : myEntity
约束的限制
- 不支持操作符约束
- 不支持or条件
- 委托和枚举类型约束是无效的
- 构造器约束值针对默认构造器
泛型方法public static T Max<T> (T first, params T[] values)
使用委托的冒泡排序
using System;namespace Console_Test{//声明委托public delegate bool ComparisonHander(int first, int second);class Program{//声明一个方法//针对不同的排序方式可以声明多个不同的方法分别调用, 只要符合委托格式public static bool Graterthan(int first, int second){return first > second;}//冒泡排序public static void BubbleSort(int[] items, ComparisonHander comparisonMethod){int i;int j;int temp;if (comparisonMethod == null){throw new ArgumentNullException("comparisonMethod");}if (items == null){return;}for (i = items.Length - 1; i >= 0; i--){for (j = 1; j <= i; j++){if (comparisonMethod(items[j - 1], items[j])){temp = items[j - 1];items[j - 1] = items[j];items[j] = temp;}}}}static void Main(string[] args){int i;int[] items = new int[5];for(i=0; i<items.Length; i++){Console.Write("Enter a integer: ");items[i] = int.Parse(Console.ReadLine());}//使用委托BubbleSort(items, Graterthan);for(i=0; i<items.Length; i++){Console.WriteLine(items[i]);}}}}
//使用Lambda表达式的冒泡函数调用//...//语句LambdaBubbleSort(items, (int first, int second) => {return first > second;});//表达式LambdaBubbleSort(items, (first,second) => first > second);
- [规范] 避免在新的代码中使用匿名方法语法, 应该优先使用更简洁的Lambda表达式