Usage of the new keyword in C #

Source: Internet
Author: User

New will open up space in the heap memory and initialize it, and then return its reference. If new is not used, such as car C1, C1 is actually only a tag on the stack (it should be 4 bytes) without complete content.

 

The oo idea has been widely used in software development projects. The most important feature is inheritance. Recently, I briefly reviewed the inheritance feature in C, the keywords that need to be used, including some key points, are specially sorted out for your convenience.

I. in C #, the keyword "new" is frequently used and has three main functions:

A) is used as an operator to create an object and call constructor.

B) as modifier.

C) used to constrain the types of parameters that may be used as type parameters in generic declarations.

In this article, we will only introduce the function of new as a modifier. When used as a modifier, The New Keyword can hide the method of the base class in the derived class, that is to say, the method that uses the derived class calls the method defined by the New Keyword, rather than the method of the base class. If you do not use the new keyword to hide the base class method, the compiler will receive a warning prompting you to use the new keyword to hide the base class method.

Note that the new and override keywords are mutually exclusive. It cannot be used at the same time.

2. The override keyword mainly provides a new implementation of the derived class to the base class method. The overwritten base class method must have the same signature as the override method, this keyword cannot be used to override non-virtual and static methods. The keywords used with this keyword are virtual, abstract, and override. At the same time, the override method cannot modify the accessibility of the virtual method. The override method and the virtual method must have the same access modifier, you cannot use modifiers new, static, virtual, or abstract to modify the override method.

The following is a small demo that shows the essential differences between new and override:

 

Class Program

{

Static void main (string [] ARGs)

{

Contact CT1 = new class1 ();

Contact CT2 = new class2 ();

Ct1.prinf ();

Ct2.prinf ();

}

}

Abstract Public class contact

{

Public Virtual void prinf ()

{

Console. writeline ("this is a virtual method ");

}

}

Public class class1: Contact

{

Public override void prinf ()

{

Console. writeline ("this is a new method ");

}

}

Public class class2: Contact

{

Public new void prinf ()

{

Console. writeline ("this is another new method ");

}

}

The running result of this demo is:

This is a new method.

This is a virtual Method

3. The virtual keyword allows you to override these objects in a derived class. By default, the method is non-virtual and cannot be rewritten, virtual keywords cannot be used with static, abstract, private, or override. The virtual keyword is closely related to override. To implement the virtual method, you must use the override or New Keyword (new and override generate different mechanisms ).

Conclusion: The New Keyword is mainly used to distinguish between a derived class and a method with the same name as the base class. By hiding the base class method, the compiler can call the correct method. Override is mainly used

And virtual methods.

 

Some time ago, a friend asked about several usage of the New Keyword of C #. Although this little guy is often used in daily programming, he did not pay attention to the usage of the new keyword, now, I will write down the summary from the Internet for my colleagues to learn.

(1) the new operator is used to create objects and call constructors.

(2) The new modifier is used to hide the inherited members of the base class members.

(3) The new constraint is used to restrict the types of parameters that may be used as type parameters in a generic declaration.

New operator

1. Used to create objects and call Constructors

Example: class_test myclass = new class_test ();

2. It is also used to call the default constructor for the value type.

For example, int Myint = new int ();

Myint is initialized to 0, which is the default value of int type. This statement is equivalent to: int Myint = 0;

3. The new operator cannot be overloaded.

4. If the new operator fails to allocate memory, it will cause an outofmemoryexception.

New Modifier

Use the new modifier to explicitly hide the members inherited from the base class. To hide an inherited Member, declare the member in the derived class with the same name and modify it with the new modifier.

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.