C # several important keywords learned

Source: Internet
Author: User

I learned a few keywords during the course of C # yesterday. I made a summary today. I can't help but share them with you. They are the This keyword, the Base keyword, the New Keyword Override and the Overload keyword 1.New is an example in our daily life. We think of computers as a class. If you want to buy a computer, you need to use the New keyword, if you are buying a lenovo computer, it should be like this: Computer lenovo = new computer. If you want to buy an iphone, it should be like this: computer iphone = new computers, whether ordinary computers or more advanced iphone computers, they are indispensable for keyboard, mouse, display screen, power supply, and so on. These are their common attributes. C #, A encapsulated function code block can be used as a class. However, you cannot directly use it because the object-oriented concept does not allow you to modify the content of a class at will. Therefore, to implement this function in C #, You need to instantiate a class. Just like a computer, if you want to use it, there is no problem at all, but you cannot modify some of the basic nature of the computer without permission. For example, you cannot talk about the computer's keyboard. However, you are still stubborn and think that you can meet your needs without a mouse on the keyboard. At this time, you should instantiate it, the system will give you a model with general functions of the computer. You should give it a name by yourself. In the future, you will be responsible for this computer, unload the keyboard, and unplug the mouse, you can make decorations on your computer, but you must add them to your instance. You cannot add them to the class. If you add them to the class, what should I do if someone else wants to type another computer, just like your computer, without a keyboard? I wonder if this example will help you understand it? In C #, The new Keyword can be used as an operator or modifier. However, I do not understand the modifier or part of it. The following describes the new operator 1. for example, [html] <span style = "font-size: 18px;"> Class_Test MyClass = new Class_Test (); </span> 2. [html] <span style = "font-size: 18px;"> int myInt = new int (); </span> myint is initialized to 0, which is the default value of the int type. This statement is equivalent to: int myint = 0; 3. the new operator cannot be overloaded. 4. if the memory allocated by the new operator is invalid, it will cause an OutOfMemoryException. base (the base can call its Base class constructor when creating a derived class instance or call a method that has been overwritten by other methods on the base class) (1 ). base calls the base class constructor [ht Ml] <span style = "font-size: 18px;"> public class A {public A () {Console. writeLine ("Build A") ;}} public class B: A {public B (): base () {Console. writeLine ("Build B");} static void Main () {B B B = new B (); Console. readLine () ;}</span> [html] so that both BuildA and BuildB can be output (2 ). base calls the method [html] style = "font-size: 18px;"> public class A {public virtual void Hello () {Console. wiriteLine ("Hello ");}} Public class B: A {public override void Hello () {base. hello (); // call the method of the base class to display Hello Console. wiriteLine ("World") ;}</span> If the program calls B. hello (), the output result is neither Hello nor World, but Hello World 3. this (used to solve the same name problem) [html] <span style = "font-size: 18px;"> class Employee {public void SetEmpName (string EmpName) {<span style = "color: # ff6666;"> EmpName = EmpName; // The statement has a problem. </span>} private string EmpName; // a member of Employee Variable} </span> the appeal code is intended to specify the EmpName parameter passed in by SetEmpName to the member variable EmpName of the Employmentyee class. However, this is not successful because the system does not know that the first EmpName in the appeal question statement refers to a class member. This keyword is used at this time. The solution is to change the red code in the above example to: This. EmpName = EmpName it is worth noting that this 4 cannot be used in static methods. override and overload Override are inherited. If the function you write is the same as the feature of the function to be inherited, add this keyword, when this function of this subclass is used, the function of the parent class (or superclass) is invisible and overwritten. For example, if Derived inherits Base and Base has void A (int a), if Derived considers A to be poorly written or not suitable for this class, if you want to re-write the code in A, write override void A (int a) so that the original function will be overwritten by the new one. Overload is a heavy load, that is, the function name is the same, and the function features are different, the system will adjust the corresponding function according to the parameters you provide. For example: void A (int a) and void A (int a, int B) if you use A (1), the first one is called. If it is) the second one is called. Note: The rewrite base method must have the same signature as the rewrite method. You cannot override non-virtual or static methods. The override base method must be virtual, abstract, or rewritten. That is to say, the methods in the base class that are rewritten with the override modifier must be virtual, abstract, or override methods. Method overload occurs when the class contains two methods with the same name but different signatures. [html] <pre name = "code" class = "html"> <pre name = "code" class =" html "> <pre> </pre> <pre>> </pre> <pre> </ pre> </pre>

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.