C # string processing, object reference and inheritance (Tenth day ),

Source: Internet
Author: User

C # string processing, object reference and inheritance (Tenth day ),

It's another week. Now it's time to sum up. Let's continue to summarize what we 've learned in cloud and college.

Theory:

Difference between StringBuilder and StringA new instance is generated when String operations (such as value assignment and concatenation) are performed, while StringBuilder does not. Therefore, it is best to use StringBuilder when splicing a large number of strings or frequently performing operations on a certain String. Do not use String. If you want to operate a growing String, try not to use the String class, instead use the StringBuilder class. The two classes work in different ways: the String class is a traditional way of modifying strings. It can indeed add a String to another String, but in.. Because the system first writes two strings to the memory, then deletes the original String object, creates a String object, and reads the data in the memory and assigns it. This process takes a lot of time. The StringBuilder class under the System. Text namespace is not like this. It provides the Append method, which can be used to modify strings in the existing object, which is simple and direct. Of course, the efficiency difference between the two is usually not noticed, but if you want to add a large number of operations to a string, the time consumed by the StringBuilder class and the String class are not an order of magnitude. Object ReferenceInt, decimal, bool, double byte enumeration structures, and Other types (value types) are transmitted copies; objects (reference types) are transmitted references. Object String reference type array class passed between methods

If the structure is written to the constructor, all fields in the structure must be assigned a value.

If the constructor is written in the structure, the default constructor is different from the class.

The constructor with parameters is written in the class, and the constructor without parameters is killed directly.

In addition, you do not need to assign values to all fields in the constructor of the class.

Difference between value type and reference type1. When a value type variable is assigned to another value type variable, the included values are copied. The value assignment of the reference type variable only copies the reference to the object, instead of copying the object itself. 2. The value type cannot be derived from a new type: All value types are implicitly derived from System. ValueType. But what is the same as the reference type is that the structure can also implement interfaces. 3. The value type cannot contain null values: 4. Each value type has an implicit default constructor to initialize the default value of this type. Stack and stack differences Stack is the memory space allocated during compilation. Therefore, your code must have a clear definition of the stack size. Local value type variables and value type parameters are all in the stack memory. Heap is the memory space dynamically allocated during the program running. You can determine the size of heap memory to be allocated based on the program running status.

Practice
Use of StringBuilder

Receives user input strings and outputs the characters in the opposite order of input. "Abc" → "CBA"

Receives an English sentence from the user and returns the words in reverse order. "Hello c sharp" → "sharp c hello"

Retrieve username and domain name from Email

Multiple article titles and authors are stored in text files. The titles and authors are separated by several spaces (a certain number of characters). Each line has one header, and the headers have long and short headers, when the title is output to the console, the title length is up to 10. If the title length is greater than 10, the Child string with the length of 8 is truncated and "... add a vertical line to output the author's name.

Write a Teacher class and a Student class respectively (use of inheritance)

First define a parent class Person

Define the Teacher Class and Student class respectively.

Call the method in the Main function

 

Now, let's write it here today. Continue tomorrow... Come on!

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.