c#学习笔记

    xiaoxiao2021-03-25  110

    

    1. 输入prop,再按两次TAB会自动添加一个属性(set/get方法)

    2. 统一命名空间内,类可以跨多个文件,需要用partial  e.g. partial class XXXX{}   3. 类型  ulong 64无符号整数  decimal 有效数字精度为28的小数类型(128bit)  byte 8位无符号整数  sbyte 8位有符号整数   4. 静态方法不要求有实例,静态类的所有方法也必须是静态的

    5. 引用参数在方法的声明和调用都要使用ref修饰符,而且必须是变量  ·引用参数不在栈中分配新的内存  ·形参的名称相当于实参变量的别名,引用与实参相同的内存位置   6. 如果不再有任何引用,对性爱那个就会被垃圾回收  要让一个对象留在堆中,  它必须被引用   7. 字典  Dictionary<TKey,TValue> kv = new Dictionary <TKey,TValue>();  kv.Add(... , ...);   8. 集合类型  任何实现了IEnumerable<T>接口的对象都可以创建List,Queue,Stack。

    Stack<string> myStack = new Stack<string>(); myStack.Push(...); // 栈转换成队列 Queue<string> myQueue = new Queue<string>(myStack); // 队列转换成列表 List<string> myList = new List<string>(myQueue); // 列表转换成栈 Stack<string> anotherStack = new Stack<string>(myList);   9. IEnumerable<T>  所有的列表和数组都实现了一个IEnumerable<T>的接口,它包含一个方法GetEnumerator()来提供循环机制,  使你可以按顺序循环处理列表    foreach循环就是利用IEnumerable<T>接口实现的  foreach( Duck duck in ducks)  {   Console.WriteLine(duck);  }    后台相当于↓    IEnumerator<Duck> emuerator = ducks.GetEnumerator();  while(enumerator.MoveNext())  {   Duck duck = enumerator.Current;   Console.WriteLine(duck);  }   10. 文件读写  // 路径以@开头,不会把\当作转义序列的开始  StreamWriter(/StreamReader) sw = new StreamWriter(@"C:\aaaa.txt");    StreamReader只是用来读取文本文件,其中只能包含值小于128的字节   11. IDisposable接口  IDisposable接口只有一个方法,Dispose()告诉对象释放为他分配的资源    // 每个流都有一个Dispose()方法,所以在一个using语句中声明流,他就会自行关闭。  using (StreamReader reader = new StreamReader("..."));   12. 将对象串行化到一个文件时,他们会以二进制格式写入文件。  对象串行化时,它引用的所有对象也要串行化(serialization)  BinaryFormatter formatter = new BinaryFormatter();  using (Stream output = File.Create(@"C:\Users\151995\Desktop\asd.txt"))     {         formatter.Serialize(output,......);     }         using (Stream input = File.OpenRead(@"C:\Users\151995\Desktop\asd.txt"))     {         string obj = (string)formatter.Deserialize(input);     }         需要在类前加入[Serializable]属性才可以进行串行化     13. 将string,char,int,float写入文件之前都可以编码为字节数组,BinaryWriter自动的对数据进行编码。  只需创建一个FileStream将他传入BinaryWriter的构造函数,然后调用BinaryWriter的方法来写数据。  using(FileStream output = File.Create("binary.dat"))  using(BinaryWriter writer = new BinaryWeiter(output))  {   writer.Write(...);  }    using(FileStream input = File.OpenRead("binary.dat"))  using(BinaryReader reader = new BinaryReader(input))  {   int intRead = reader.ReadInt32();   // ReadString(),ReadBytes(xx)等对应每个值类型,   byte[] byteRead = reader.ReadBytes(4); // 都有一个相应的方法返回正确类型的数据,大多数不需要参数,   float floatRead = reader.ReadSingle(); // 不过ReadBytes(xx)需要告诉要读几个字节  }

    14. File.OpenWrite()不会删除文件,只是从开始处覆盖原来的数据  而File.Create()会创建一个新文件   15. StreamReader用来读写文本,是从流读取字符,  而Stream.Read方法从流中直接读取字节,处理字节和字节数组。

     using(Stream input = File.OpenRead(args[0]))  {   int position = 0;   byte[] buffer = new byte[16];   while(position < input.Length)   {    int charactersRead = input.Read(buffer, 0, buffer.Length);        // 这是一个将字节数组转换成串的简便方法    string bufferContents = Encoding.UTF8.GetString(buffer);   }  }   16. 使用File类的ReadAllText(),ReadAllText(),ReadAllBytes(),ReadAllLines()  都会自动打开和关闭流,不需要Close()方法。   17. File和FileInfo  File中的方法是静态的,不需要创建实例就可以调用。  FileInfo要求必须用一个文件名来实例化对象。  如果需要对同一个文件完成多个文件操作FileInfo会更方便,因为只用传递一次文件名。

    转载请注明原文地址: https://ju.6miu.com/read-13342.html

    最新回复(0)