[关闭]
@laofang 2016-06-12T15:53:59.000000Z 字数 8199 阅读 1332

C#笔记

.Net


C#本质论(第四版)

挑选了一些看书是看起来比较有意思的东西记录一下. 重新看发现自己的笔记有点乱, 没头没尾的. 但是, 不得不说这真是本好书. 很好看懂.
看这本书,不求都懂, 对C#教之前能有一个大局观上的认识提升.
书看起来挺厚, 但是通俗易懂.
适合作为入门书籍
不适合作为工具书

第一章 概述

第二章 数据类型

第三章 操作符和控制流

第四章 方法和调用

  1. static string Combine(params string[] paths)
  2. {
  3. string result = string.Empty;
  4. foreach(string path in paths)
  5. {
  6. result = System.IO.Path.Combine(result,path);
  7. }
  8. }
  1. static void Main()
  2. {
  3. DisplayGreeting(firstName:"Inigo", lastName:"Montoya");
  4. }
  5. public void DispaltGreeting(
  6. string firstName,
  7. string middleName = default(string),
  8. string lastName = default(string))
  9. {
  10. //............
  11. }
  12. //便利: 如果一个方法许多参数都有默认值, 这种做法极大方便编程
  13. //代价: 牺牲接口灵活性: 方法参数名改变会带来编译错误

第五章 类

  1. class User
  2. {
  3. //字段
  4. private string _Name;
  5. //属性
  6. public string Name
  7. {
  8. get{return _Name;}
  9. set{_Name = value;}
  10. }
  11. //自动实现的属性
  12. public string age{get;set;}
  13. }
  14. //调用
  15. Class Program
  16. {
  17. static void main()
  18. {
  19. User user = new User();
  20. User.Name = "Zhangsan";
  21. Console.WriteLine(User.Name);
  22. }
  23. }
  24. /**规范
  25. 考虑为支持字段和属性使用相同的大小写风格
  26. 考虑属性和它的类型通明
  27. 避免使用camel风格命名字段
  28. 不要声明public或者protected的字段, 而应该声明private然后通过属性公开
  29. 有限使用自动实现的属性而不是字段
  30. 如果没有额外的实现逻辑, 要优先使用自动实现的属性而不是编写属性的完整版本
  31. **/
  1. class Employee
  2. {
  3. static Employee()
  4. {
  5. Random randomGenerator = new Random();
  6. NextId = randomGenerator.Next(101,999);
  7. }
  8. public static int NextId = 42;
  9. //执行结果: 先执行声明语句:NextId = 42, 随后访问构造器: NextId 等于一个随机值
  10. //另外: 普通的构造器(实例构造器)执行的结果也是这样
  11. }
  1. //File: a.cs
  2. partial class Program
  3. {
  4. }
  5. //File: b.cs
  6. partial class Program
  7. {
  8. }

第六章 继承

  1. public class program
  2. {
  3. public static void Main()
  4. {
  5. //假设Contact是PdaItem的子类
  6. Contact contact = new Contact();
  7. //隐式转型
  8. PdaItem item = contact;
  9. //显示转型,而且显示类型是必须的
  10. contact = (Contact)item;
  11. }
  12. }
  1. public class PdaItem
  2. {
  3. //不添加 virtual 关键字会报错
  4. public virtual string Name{get;set;}
  5. }
  6. public class Contact:PdaItem
  7. {
  8. //不添加override关键字会报严重警告
  9. public override string Name
  10. {
  11. get{ return FirstName + " " + LastName; }
  12. set
  13. {
  14. string[] names = value.Split(' ');
  15. FirstName = names[0];
  16. LastName = names[1];
  17. }
  18. }
  19. public string FirstName{get;set;}
  20. public string LastName{get;set;}
  21. }
  22. //"运行时"遇到虚方法时, 会调用虚成员派生的最远的重写即 item.Name = "..."会执行Contact类中的方法--->虚方法不应该包含关键代码

第七章 接口

第八章 值类型

  1. struct Angle
  2. {
  3. public int Degress { get;}
  4. public int Minutes { get; }
  5. public int Seconds { get; }
  6. //结构体只能声明有参构造器, 不能定义无参构造器
  7. public Angle(int degress, int minutes, int seconds)
  8. {
  9. Degress = degress;
  10. Minutes = minutes;
  11. Seconds = seconds;
  12. }
  13. //返回一个新的值类型
  14. public Angle Move(int degress, int minutes, int seconds)
  15. {
  16. return new Angle(
  17. Degress + degress,
  18. Minutes + minutes,
  19. Seconds + seconds);
  20. }
  21. }
  1. class DisplayFibonaci
  2. {
  3. static void Mian()
  4. {
  5. int totalCount;
  6. System.Collection.ArrayList list = new System.Collection.ArrayList();
  7. Console.Write("Enter a numner between 2 and 1000:");
  8. totalCount = int.Parse(Console.ReadLine());
  9. list.Add((double)0);//装箱, Add方法的参数是object
  10. list.Add((double)1);//装箱
  11. for(int count = 2; count < totalCount; count ++)
  12. {
  13. //拆箱(两次): 加法运算需要值类型
  14. //装箱: Add方法参数类型为引用类型
  15. list.Add(((double)list[count-1] + (double)list[count-2]));
  16. }
  17. //拆箱: list内容为object, 赋值给double类型的count需要拆箱
  18. foreach(double count in list)
  19. {
  20. //装箱: Console.Write(string format, object args)
  21. Console.Write("{0}\t",count);
  22. }
  23. }
  24. }
  1. enum ConnectionSate : short//修改默认基础类型
  2. {
  3. DisConnected, //0
  4. Connecting = 10, //10 --- 修改默认值
  5. Connected, //11
  6. Joined = conected, //11
  7. Disconnecting //12
  8. }

第九章 良构类型

  1. public struct Coordinate
  2. {
  3. public Coordinate(Longitude longitude, Latitude latitude)
  4. {
  5. _Longitude = longitude;
  6. _Latitude = latitude;
  7. }
  8. public Longitude Longitude{get{return _Longitude;}}
  9. privete readonly Longitude _Longitude;
  10. public Latitude Latitude{get{return _Latitude;}}
  11. privete readonly Latitude _Latitude;
  12. //重写ToString方法
  13. public override string Tostring()
  14. {
  15. return string.Formate("{0} {1}",Longitude, Latitude);
  16. }
  17. }
  1. public static bool operator ==(){...}
  2. public static object operator + (){...}
  3. 例: 检查坐标是否相等
  4. public static bool operator ==(Coordinate leftHandSide, Coordinate rightHandSide)
  5. {
  6. //Check if leftHandSide is also null
  7. //operator == would be recursive--使用 == 来检查null会造成死递归
  8. if(ReferenceEquals(leftHandSide,null))
  9. {
  10. return ReferenceEquals(rightHandSide,null);
  11. }
  12. return leftHandSide.Equals(rightHandSide));
  13. }
  1. public staic bool operator true(IsValid item){...}
  2. public tatic bool operator false(IsValid item){...}
  1. //在Latitute和double之间提供隐式转换
  2. public struct Latitude
  3. {
  4. //------转换准备
  5. privete readonly double _DecimalDegress;
  6. public Latitude(double dicimalDegress)
  7. {
  8. _DecimalDegress = Normalizi(decimalDegress);
  9. }
  10. public double DecimalDegress
  11. {
  12. get{return _DecimalDegress;}
  13. }
  14. //-------转换
  15. public static implicit operator double(Latitude latitude)
  16. {
  17. return latitude.DecimalDegress;
  18. }
  19. public static implicit operator Latitude(double degress)
  20. {
  21. return new Latitude(degress);
  22. }
  23. }
  24. //
  1. win: csc.exe /r:cooridnates.dll Program.cs
  2. mono: msc.exe /r:cooridnates.dll Program.cs
  3. *
  1. /doc:sss.xml sss.cs --- /doc:目标文件名 原文件名

  1. //利用终结器清理资源:语法 ~类名
  2. class FileHelper
  3. {
  4. //构造器
  5. public FileHelper(){...}
  6. //终结器:--不能带参数 终结器不能显示调用, 由运行时在恰当的时间运行
  7. //对象最后一次使用之后并在程序正常关闭之前
  8. ~FileHelper(){....}
  9. }

第十章 异常处理

第十一章 泛型

  1. //接口
  2. interface A<T>
  3. //类
  4. class B<T>
  5. calss C<T> : A<T>
  6. //接口约束
  7. //---------规定I必须实现IComparable接口
  8. class BinaryTree<T> where T: System.IComparable<T>
  9. //---规定value必须为自定义实体类型
  10. class myDic<TKty,TValue> where TValue : myEntity

第十二章 委托和lambda表达式

  • [规范] 避免在新的代码中使用匿名方法语法, 应该优先使用更简洁的Lambda表达式

第十三章 事件

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注