C # several important keywords learned

Source: Internet
Author: User
Tags lenovo

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, new Keyword override and overload keyword


1. New

 


Let's give an example in our life. We think of computers as a class. If you want to buy a computer, you need to use the new keyword. If you want to buy a computer

The Lenovo computer should be like this: Computer Lenovo = new computer

If you want to buy an iPhone, it should be like this: PC iPhone = new computer

Keyboard, mouse, display, power supply, and so on are all common properties of ordinary computers and even more advanced iPhone computers.


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 can name it yourself.
In the future, you will be responsible for this computer. You can unload the keyboard, unplug the mouse, and make some decorations on the computer, but this will all be added to your instance, you cannot add it to a class. If you add it to a class, when someone else instantiates it, it will be the same as your computer, and there will be no keyboard, what should someone else do if they want to type?


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 don't understand the modifier or part of it. The following section describes how to perform operators.

Summary


New operator


1. Used to create objects and call Constructors

Example:

Class_Test MyClass = new Class_Test(); 


2. Used to call the default constructor for the Value Type

Example:

int myInt = new int(); 


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 fails, it will cause an outofmemoryexception.

2. base (the base class can call its base class constructor or call the base class

Other methods)


(1) Base calls the base class Constructor

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 = new B(); Console.ReadLine();       }}
 

In this way, both builda and buildb can be output at the same time.

(2) Base calls the method whose base class has been overwritten.

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 ");}}

If the program calls B. Hello (), the output result is neither Hello nor world, but hello World.

3. This (used to solve the problem of the same name)


Class employee {public void setempname (string empname) {empname = empname; // This statement has a problem} private string empname; // member variable of employee}


The purpose of the Appeal code is to specify the empname parameter passed 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 is used at this time.

Keyword. The solution is to change the red code in the previous example to: This. Empname = empname

It is worth noting that this cannot be used in static methods.

4. Override and overload


Override is inherited. If the function you write is the same as the feature of the function to be inherited, add this keyword to use this

When this function of sub-classes is used, the function of the parent class (or super class) is invisible and overwritten.

For example, if derived inherits base and base has void a (int A), if you think a is not well written or not suitable for this

Class, you want to re-write the code in A, then write override void a (int A) so that the original function will be overwritten by the new one.

Dropped.


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 a (), the second one is called.


Note:

  • The override base method must have the same signature as the override 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.

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.