. NET Learning notes----2015-06-25 (read-write files for file classes, list generic collections, boxing and unboxing, Dictionary dictionary collections)

Source: Internet
Author: User

File class: Static class, Create Delete Copy Move, primarily used to read and write data to a text file

File class: Disadvantage: Only small files can be read and written

Read and write operations:

            //byte[] buffer = file.readallbytes (@ "C:\Users\Administrator\Desktop\new.txt");            ////Each element in a byte array is decoded into a string according to the encoding we specify////utf-8 GB2312 GBK ASCII Unicode            //string s = Encoding.UTF8.GetString (buffer); //Console.WriteLine (s); //Console.readkey (); //without this file will create one, and some will overwrite            stringstr ="The weather is fine today, and the scenery is good everywhere ."; //need to convert a string into a byte array            byte[] buffer =Encoding.Default.GetBytes (str); File.writeallbytes (@"C:\Users\Administrator\Desktop\new1.txt", buffer); Console.WriteLine ("Write Success"); Console.readkey ();

1. Absolute path and relative path

Absolute path: The file can be found directly on the computer by a given path

Relative path: The path of the file relative to the application

*** Use relative paths whenever possible in the development process * * *

2. List generic Collection

//Creating generic Collection Objectslist<int> list =Newlist<int>(); List. ADD (1); List. ADD (2); List. ADD (3); List. AddRange (New int[] {1,2,3,4,5,6 }); List.            AddRange (list);  for(inti =0; I < list. Count; i++) {Console.WriteLine (list[i]);            } console.readkey (); //list Generic collections can be converted to arrays//int[] nums = list.            ToArray (); //list<string> liststr = new list<string> (); //string[] str = Liststr.toarray ();            Char[] CHS =New Char[] {'C','a','b' }; List<Char> Listchar =CHS.            ToList ();  for(inti =0; i < Listchar.count; i++) {Console.WriteLine (listchar[i]);            } console.readkey (); //list<int> listtwo = nums. ToList ();

3. Packing and Unpacking

Boxing: Converting a value type to a reference type

Unboxing: Converting a reference type to a value type

Avoid packing and unpacking as much as possible during the development process, which can affect operational efficiency.

Look at whether the two types have occurred boxing or unpacking, to see if there is an inheritance relationship between the two types.

If there is an inheritance relationship, boxing or unpacking may occur;

If there is no inheritance relationship, the loading and unpacking will not happen;

4. Dictionary Dictionary Collection

Similar to Hashtable, but dictionary keys and values are directly fixed to a good type such as Dictionary<int, string>

dictionary<int,string> dic =Newdictionary<int,string>(); Dic. ADD (1,"Zhang San"); Dic. ADD (2,"John Doe"); Dic. ADD (3,"Harry"); //the way of dictionary traversal            foreach(keyvaluepair<int,string> KVinchdic) {Console.WriteLine ("{0}----------{1}", KV. Key, KV.            Value); } console.readkey ();

. NET Learning notes----2015-06-25 (read-write files for file classes, list generic collections, boxing and unboxing, Dictionary dictionary collections)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.