The use of destructors, namespaces, and Strings in C # (ninth day)

Source: Internet
Author: User
Tags ticket

Again to summarize the knowledge of the time, today in the cloud and college learning the destructor, namespace and string processing, now for everyone to summarize down.

Theory:

Destructors

Destructors cannot be defined in structs. Destructors can only be used on classes. A class can have only one destructor. Destructors cannot be inherited or overloaded. Destructors cannot be called. They are automatically called. Destructors have neither modifiers nor parameters. name Spacenamespace (namespace), used to resolve class name problems, can be seen as "folders of classes." • If the code and the class being used are in a namespace, the using is not required. • There are two methods for class invocation under different namespaces: – Write the full name namespace. Class Name – The using reference namespace, and then call (note: The using can declare the introduction of namespace and can also implement the release of unmanaged resources, A class that implements the idisposiable is created in the using, and the Dispose method of the object is automatically called when the using ends, freeing the resource. ) string ProcessingString can be seen as a read-only group of Char. char c = string in s[1]; C# has an important attribute: Immutability, the string once declared can no longer be changed. Therefore, only char at the specified position can be read by index and cannot be modified for char at the specified position. *If you want to modify char, you must create a new string with S. The ToCharArray () method gets a char array of strings, modifies the array, and calls the new string (char[]) to create a string of char arrays (without a fine-grained study). Once the string is created, the modification of the char array does not cause a change in the string. • Connection of Strings +, as long as one is a string type and the other is automatically converted to a string type • All types can be called. The ToString () method is converted to a string type. common methods of the string class S1. Equals (s2,stringcomparison.ordinalignorecase),Two strings are compared in a case-insensitive comparison. • ToLower ():Gets the lowercase form of the string. (This has been summed up in the previous) · ToUpper ():Gets the uppercase form of the string; Trim () removes whitespace at both ends of the string. (This has been summed up before) • Note that strings are immutable, so these functions do not directly alter the contents of the string, but instead return the value of the modified string in the form of a function return value. S.tolower () and s=s.tolower () • Distinguish between the variable name and the value that the variable points to. There can be a lot of strings in the program, and then the string variables point to them, and the variables can point to other strings, but the strings themselves do not change. String immutability refers to an immutable string in memory, not a variable. Segmentation of Stringsstring[] Split (params char[] separator):Splits a string into an array of strings according to the specified delimiter; string[] Split (char[] separator, stringsplitoptions options)Splits a string into an array of strings according to the specified Char delimiter (options take removeemptyentries to remove the blank string in the result); A detailed description of string functions• String substitution: String replace (String oldValue, String newvalue) replaces the occurrence of oldValue in a string with NewValue. Example: Name substitution. • Take substring: string Substring (int startIndex), take the substring starting from position startIndex to the last, string Substring (int startIndex, int length), Takes a substring starting from position startindex, length, if the substring is not long enough to give an error. bool Contains (string value) determines whether the string contains a substring value bool StartsWith (string value) determines whether the string starts with a substring of value; bool EndsWith (string Value) determines whether the string ends with a substring of value; int IndexOf (string value): Takes the position where the substring value first appears. int IndexOf (string value,int startIndex) Real:add a constructor method to the student class that was written earlier. Enables you to assign a value to a name's gender-age language number by constructing a method when instantiating a student class, or to assign values only to names and genders. The default age is 18 years, and the default is 0 for English. – Zhang Three men 183 subjects: – Small Sutherland 163 grades:* First in defining a class
Class Student    {//Add a constructor method to the student class that was written earlier. The method can be used to assign values to the names of the gender-age language, such as the number of English attributes, or only the name and gender. The default is 18 years old, the number of English results by default is 0.//Zhang Sanman 18  the results of the three subjects are: 90 95 80//Small Sutherland  three subjects: a        .        string gender;        int age=18;        int Chinese;        int math;        int 中文版;        Public Student (String n,int a,string g,int c,int m,int e)        {            name = N;            age = A;            gender = g;            Chinese = c;            math = m;            中文版 = e;               }        Public Student (string n, string g, int c, int m, int e)        {            name = N;            gender = g;            Chinese = c;            math = m;            中文版 = e;        }        public void Stt ()        {            Console.WriteLine ("Hello everyone, my name {0}, is {1} classmate, this year {2} years old, my three subjects are: {3},{4},{5}", name, gender, age, Chinese, math, 中文版);        }           }

Write in the main function.

static void Main (string[] args)        {            Student stu1 = new Student ("Zhang San", 18, "male", 90,95,80);            Student STU2 = new Student ("Xiao Lan", 16, "female", 85,100);            Student stu3 = new Student ("Xiao Lan", "female",------);            Stu1. Stt ();            Stu2. Stt ();            Stu3. Stt ();            Console.readkey ();        }

The operating result is:

• Write a ticket class that has a distance attribute (this property is read-only, assigned in the constructor method), cannot be negative, has a price attribute, the price attribute is read-only, and calculates the price (1 yuan/km) by distance distance: – 0-100 km fare is not discounted – 101-200 km Total hits 95 percent – 201-300 km total 90 percent – 300 kilometres or more 80 percent

There is a way to display information about this ticket. 90 km, 90 bucks.

• Test the class above.
Class Ticket {#region constructor public Ticket (int distance) {if (distance<0)          {distance = 0;        } this.distance = distance;        } #endregion int distance;                public int Distance {get {return Distance;        }} double price=500;                 Public double Price {get {if (distance<=100 && distance>=0) {                            return distance * 1.0;            } else if (distance>=101 && distance<=200) {return distance * 0.95; } else if (distance>=201 && distance<=300) {return distance *            0.9;            } else {return distance*0.8; }}} public void Show () {           Console.WriteLine ("{0} km need {1} money", Distance,price); }    }
static void Main (string[] args)        {            Ticket t1 = new Ticket (a);            Console.WriteLine (t1. Price);            Console.readkey ();        }

Test Result: This question does not know where the problem occurs, the results are not applied to the Show method

randomly enter a name that you think of in your mind, and then output the length of its string as long: the length of the string can be

Two participants enter their favorite course names, judging if they are the same, and if they are equal, output you both like the same course. If they are not the same, you will be exporting courses that you both prefer.

Let the user enter a date format such as: 2008-01-02, you output the date you entered is January 2, 2008

  static void Main (string[] args)        {                      Console.WriteLine ("Please enter Date");            String date = Console.ReadLine ();            string[] STRs = date. Split ('-');            String datetine = Strs[0] + "year" + strs[1] + "month" +strs[2]+ "Day";            Console.WriteLine (datetine);            Console.readkey ();         }

Let's summarize here today, and continue studying in the next Monday. Come on!

The use of destructors, namespaces, and Strings in C # (ninth day)

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.