C # Object-oriented summary 2

Source: Internet
Author: User

1. Value types and reference types:

Value types: int, double, bool, char, decimal, struct, enum

Reference types: String, custom class, array

Storage: Values of value types are stored in the stack of memory.

The value of a reference type is stored in the heap of memory.

2. String

1), the immutability of the string

When you re-assign a string, the old value is not destroyed, but it re-opens a space to store the new value.

When the program finishes, the GC scans the entire memory and destroys it immediately if it finds that the space is not pointing.

2), we can say the string as a char type of a read-only group.

ToCharArray (); Convert a string to a char array

New String (char[] CHS): Ability to convert a char array to a string

3. The various methods provided by the string

1), Length: Gets the number of characters in the current string

2), ToUpper (): Convert characters to uppercase

3), ToLower (): Convert string to lowercase

4), Equals (lessontwo,stringcomparison.ordinalignorecase): Compares two strings, can ignore the case

5), Split (): Splits the string and returns an array of string types.

6), Substring (): Intercepts the string. Include the intercepted position at the time of interception.

7), IndexOf (): determines where a string first appears in the string, and if not, returns 1

8), LastIndexOf (): Determine the last occurrence of a string in a string, and, if not, return-1

9), StartsWith (): Judging by .... Begin

10), EndsWith (): Judging by ... End

11), replace (): Replace a string in a string with a new string

12), Contains (): Determines whether a string contains the specified string

13), Trim (): Remove the space before and after the string

14), TrimEnd (): Remove the trailing space in the string

15), TrimStart (): Remove the preceding space in the string

16), String. IsNullOrEmpty (): Determines whether a string is empty or null

17), String. Join (): The array is concatenated with the specified string, returning a string.

4. Inheritance

We may write some duplicate members in some classes, and we can encapsulate these repeating members individually into a class as the parent class of those classes.

Student, Teacher, Driver subclasses (derived classes)

Person Parent class (base class)

Subclass inherits the parent class, what does the subclass inherit from the parent class?

First, the subclass inherits the properties and methods of the parent class, but the child class does not inherit the private field of the parent class.

Question: Does the subclass inherit the constructor of the parent class?

A: The subclass does not inherit the constructor of the parent class, but. The subclass defaults to calling the parent class parameterless constructor, creating the parent class object so that the child class can use members from the parent class. So, if a parameter constructor is re-written in the parent class, the parameterless one is killed and the subclass is not called, so the subclass will error.

Workaround:

1), re-write a parameterless constructor in the parent class. (not commonly used)

2), the constructor for calling the parent class, displayed in the subclass, using the keyword: base ()

Inherited attributes

1, inheritance of the single root: a subclass can have only one parent class.

2. Transitive nature of inheritance

object is the base class for all classes.

5. New keyword

1), creating objects

2), hide the members of the same name inherited from the parent class.

The hidden consequence is that a subclass cannot call a member of the parent class.

6, the Richter conversion

1), subclasses can be assigned to the parent class

2), if the parent class is loaded with a subclass object, then it can be said that the parent class is strongly converted to a subclass object.

A subclass object can call a member in the parent class, but the parent class object will always be able to invoke only its own members.

is: Represents the type conversion, returns a true if the conversion succeeds, or returns a false

As: Represents a type conversion and returns the corresponding object if it can be converted, otherwise returns a null

is Usage

If (P is Student)

{

Student ss = (Student) p;

Ss. Studentsayhello ();

}

Else

{

Console.WriteLine ("Conversion failed");

}

Usage of AS

Student t = p as Student;

T.studentsayhello ();

Console.readkey ();

Protected protected: Can be accessed within the current class and within the subclass of the class.

7. Collection

A Collection object was created

ArrayList list = new ArrayList ();

Collection: A collection of many data

Arrays: Immutable lengths, single type

The benefits of the collection: the length can be arbitrarily changed type casually

ArrayList the length of the collection: when the number of elements actually contained in the collection (count) exceeds the number of elements that can be contained (capcity), the collection will request more space in memory to ensure that the length of the collection is sufficient.

ArrayList list = new ArrayList ();

Add a single element

List. ADD (TRUE);

List. ADD (1);

List. Add ("Zhang San");

Adding a collection element

List. AddRange (New int[] {1, 2, 3, 4, 5, 6, 7, 8, 9});

List. AddRange (list);

List. Clear (); Clear all elements

List. Remove (true); Deletes a single element write who will delete who

List. RemoveAt (0); Delete element according to subscript

List. RemoveRange (0, 3); Remove a range of elements according to the subscript

List. Sort ();//ascending order

List. Reverse (); reverse

List. Insert (1, "inserted"); Inserts an element at the specified position

List. Insertrange (0, new string[] {"Zhang San", "John Doe"}); Inserts a collection at the specified location

BOOL B = list. Contains (1); determine if a specified element is included

Create a collection that adds numbers, averages and values, maximum, minimum

1ArrayList list =NewArrayList ();2List. AddRange (New int[] {1,2,3,4,5,6,7,8,9 });3             intsum =0;4             intMax = (int) list[0];5              for(inti =0; I < list. Count; i++)6             {7                 if((int) List[i] >max)8                 {9Max = (int) list[i];Ten                 } OneSum + = (int) list[i]; A             } - Console.WriteLine (sum); - Console.WriteLine (max); theConsole.WriteLine (sum/list. Count); -Console.readkey ();

Write a set of length 10, which requires that 10 numbers (0-9) be stored randomly, but all numbers are required to be not duplicated.

1ArrayList list =NewArrayList ();2Random r =NewRandom ();3              for(inti =0; I <Ten; i++)4             {5                 intRnumber = R.next (0,Ten);//There is no such random number in the collection6                 if(!list. Contains (rnumber))7                 {8 list. ADD (rnumber);9                 }Ten                 Else//This random number is in the collection. One                 { A                     //once a duplicate random number has been generated, this cycle does not count . -i--; -                 } the  -             } -              for(inti =0; I < list. Count; i++) -             { + Console.WriteLine (List[i]); -             } +Console.readkey ();

8, hastable key value pairs set

In the set of key-value pairs, we are looking for values based on the key.

Key value pair Object [key]= value;

Keys must be unique in the set of key-value pairs, and values can be duplicated

A key-value pair collection object was created:

1Hashtable HT =NewHashtable ();2Ht. ADD (1,"Zhang San");3Ht. ADD (2,true);4Ht. ADD (3,'male');5Ht. ADD (false,"of the wrong");6Ht. ADD (5,"Zhang San");7ht[6] ="new here.";//This is also a way to add data8ht[1] ="Zhang San";9Ht. ADD ("ABC","CBA");TenConsole.WriteLine (ht[1]);//in the set of key-value pairs, the value is determined by the key.

9. Foreach Loop

foreach (var item in nums)

{

Console.WriteLine (item);

}

10. Path class

String str = @ "C:\3000soft\Red spider\data\message\ Lao Zhao wav";

Console.WriteLine (Path.getfilename (str));//Get file name

Console.WriteLine (Path.getfilenamewithoutextension (str));//Get the file name but do not include the extension

Console.WriteLine (Path.getextension (str));//Get the file name extension

Console.WriteLine (Path.getdirectoryname (str));//Get the name of the folder where the file is located

Console.WriteLine (Path.GetFullPath (str));//Get the full (absolute) path where the file is located

Console.WriteLine (Path.Combine (@ "C:\a\", "B.txt"));//Connect two strings as a path

The reason why the encoding format is garbled is that you save the file in a different encoding format than the one you opened.

11. Documents

File.create (@ "C:\Users\SpringRain\Desktop\new.txt"); Create a file

File.delete (@ "C:\Users\SpringRain\Desktop\new.txt"); Delete a file

File.Copy (@ "C:\Users\SpringRain\Desktop\code.txt", @ "C:\Users\SpringRain\Desktop\new.txt"); Copy a file

File.move (@ "C:\Users\SpringRain\Desktop\code.txt", @ "C:\Users\SpringRain\Desktop\newnew.txt"); cut

C # Object-oriented summary 2

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.