30 Days C # foundation Consolidation------Collection, file (document operation), encoding processing character set

Source: Internet
Author: User
Tags fread

One: generic type

About generics I am not very good understanding, but the use of concrete can be, it can be understood, we define an array, but do not know what type of value in the future, it is a contradiction, this time the generics appear, it can solve this scenario,list<t> before this is the type , as long as we know what values will be stored here, and now we don't know what kind of values to use T (type) to store in the future. This is very much used in collections, some common interfaces at the bottom of the project, and in classes.

Two: Set

Linetype Collection----list<t>

            list<int> array = new list<int> ();            int[] str = {0, 0, 0, 0,};            Array. ADD (1);            Array. ADD (2);            Array. ADD (5);            Array. AddRange (str);      Here is the addition of an array to the collection.            Array. Remove (5);           Here is the addition of a fixed number.            Array. RemoveAt (1);       The item here is the subscript of our array. We can use this to delete elements.            array[0] =;              Modify the first element,            foreach (var item in array)         //Here is a circular output about the collection.            {                Console.WriteLine (item. ToString ());            }            Console.readkey ();

Discrete sets

Dictionary<string,int> dic=new dictionary<string,int> ();

            dictionary<string, string> dic = new dictionary<string, string> ();            Initialize            dic. Add ("Zhang Hui", "1193451014");            Dic. ADD ("Ahui", "1193451014");            Dic. Add ("Xiao Hui", "1193451014");            Dic. Add ("Wang Hui", "1193451014");            BOOL A=dic. ContainsKey ("Ahui");                     Use the key to see if            Console.WriteLine (a) is stored in the collection;            This key-value pair sets---we need to know how to loop the output of            foreach (keyvaluepair<string, string> item in DIC)          // Note the type of temporary variable (item)-----keyvaluepair<string, string>            {                Console.WriteLine (item) when outputting. Key + "," + Item. Value);            }            Console.readkey ();

Three: File operations

What the file is: A memory inside the hard disk, is a bunch of bytes fast.

1: Name space

System.IO; namespace of the file

2:filestream file stream to read and write files in bytes.

The above pictures are some common methods and classes about files. Here are some exercises.

---the >filestream file stream.

1: Write about the file stream.

Let's see what we can do with a FileStream. are a few constructors.

---->writebyte () method

            FileStream-----Use a file stream to write, the first parameter is the file name, the second is the enumeration type, and we choose to write again. The third one is the choice of permissions.                        FileStream fwrite=new FileStream ("Ahui.txt", filemode.create,fileaccess.write);            Fwrite.writebyte ();              Write, but if we do not call display or flush will not write to the file, just write buffer period.            //flush ()-----Clears the buffer for this stream so that all buffered data is written to the file.            Fwrite.flush ();                        This can be written to the file.            Console.WriteLine (fWrite);            Console.readkey ();

The above code if we input from the client will appear garbled, we need to set the encoding format to modify.

----->write ()

            Console.WriteLine ("Please enter? ");            String sInput = Console.ReadLine ();            Setting the encoding format----help us to write Chinese characters to the file            byte[] bs = Encoding.Default.GetBytes (sInput);            Fwrite.write (bs,0,5);              Write, but if we do not call display or flush will not write to the file, just write buffer period.

---->readbyte ()

            Using using to write, this will finally automatically invoke the display () method, which allows us to write the contents of the buffer to the file.            using (FileStream fread=new FileStream ("Ahui.txt", FileMode.Open,FileAccess.Read))            {                list<byte> list=new list<byte> ();                Sets a collection of                int res =-1;                while ((Res=fread.readbyte ())!=-1)           //Use a loop to add the values in the file to the collection                {                     list. Add ((byte) res);                             The Add () method of the collection.                }            }                 Console.readkey ();

The 49 and 50 here are the values saved in my file.

---->read

            using (FileStream freader=new FileStream ("Ahui.txt", FileMode.Open,FileAccess.Read))            {                //Use an array to store a block of bytes                byte []bs=new byte[10];                                     This is equivalent to the buffer as                int count = Freader.read (bs,0,bs. Length);        Requires 10 bytes to be read, starting with the No. 0 bit of the array, all stored.                 }

----> Copy files

            Idea---------------Read the file first, then write the file.            Console.Write ("Please enter the path you want to copy #");            String str = Console.ReadLine (); if (!                File.exists (str))//Determine if the file exists {Console.WriteLine ("The file does not exist,, you are screwing me,");                Console.readkey (); Return            Jump straight out.            }//Go here means that the file exists Console.Write ("Please enter WHERE to copy #");            String StrM = Console.ReadLine ();                using (FileStream freader=new FileStream (str,filemode.open,fileaccess.read))//read operation {                    using (FileStream rwrite=new FileStream (strm,filemode.create,fileaccess.write))//write operation {                    The first------uses ReadByte () and WriteByte () to process int res =-1 in bytes;                    while ((Res=freader.readbyte ())!=-1)//here to see whether the value is read, whether to loop, not read to Res=-1, no write operation.                     {Rwrite.writebyte ((byte) res); Parameter can only be of type Byte, IYou need to force type conversions.            }}} Console.WriteLine ("OK"); Console.readkey ();

The efficiency above is not very high, we can use Read ()/write () to do, the efficiency is much higher than before.

                    The second------use Read () and write () in bytes to process                    //The buffer is used here, we need a byte array to handle.                    byte [] bubytes=new byte[1024*1024*10];         Applied for a space of 10M size.                                              int count = 0;                    while ((count = Freader.read (bubytes, 0, bubytes.length)) > 0)       //writes the read to buffer, and as long as there is a value in the file, count will be greater than 0.                    {                        rwrite.write (bubytes, 0, Count);                                   Writes the value in buffer to the file.                              }  

Four: encoding handles the character set.

Declares a character encoding,

Encoding en = encoding.getencoding ("gb2312"); Declares a gb2312 character encoding

Get all the character encodings in your computer

            encodinginfo[] Ens = Encoding.getencodings ();  Get all the encodings and return an array. +encodinginfo is a class that has some attributes about the encoding. for            (int i = 0; I <ens. Length; i++)            {                Console.WriteLine ("{0},{1},{2}", Ens[i]. Codepage,ens[i]. Displayname,ens[i]. Name);            }            Console.readkey ();

We can see all the encodings in the PC via the console.

There are comments in the above code, we can find some coding we can declare an array through a class, so we can point out all of our code, which has encoded the number of encodings, code names, encoding instructions.

Eg: Converts a number to 16 binary.

            for (int i = 0; i < i++)            {                Console.WriteLine (i.tostring ("X2"));           The x in this indicates that the encoded format is 16 binary. 2 indicates a 2-digit number reserved.            }            Console.readkey ();

Five: The question of the invocation of inheritance being first recognized

Subclass inherits the base class, and when the subclass is initialized, we call the constructor of the base class first, and in the call to the constructor of the subclass, there is a problem called backtracking. It's from the inside out.

    <summary>    ///base class///</summary> public class Parset {public        parset ()        {            Console.WriteLine ("The base class is called. ");        }    }    <summary>//Subclass///    </summary> public    class Childa:parset    {        private string Name = "FAI";        Public Childa ()        {            Console.WriteLine ("Subclass called,,,");        }        public void SayHello ()        {            Console.WriteLine (name);        }    }
            Childa ca=new Childa ();            Ca. SayHello ();            Console.readkey ();

We can find this rule through here.

30 Days C # foundation Consolidation------Collection, file (document operation), encoding processing character set

Related Article

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.