C # usage of the new keyword in programming

Source: Internet
Author: User
Reference http://dev.yesky.com/276/7618276.shtml
C # the usage of the new keyword in programming is as follows:
(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.

See the following classes:

The following is a reference clip:
1 public class myclass
2
3 {
4
5 Public int X;
6
7 public void invoke (){}
8
9}
10

Using the invoke name in a derived class to declare a member will hide the invoke method in the base class, namely:

The following is a reference clip:
1 public class myderivedc: myclass
2
3 {
4
5 new public void invoke (){}
6
7}
8

However, because field X is not hidden by similar names, this field is not affected.

Hiding names by inheritance takes one of the following forms:

1. Introduce constants, designation, attributes, or types in the class or structure to hide all base class members with the same name.

2. introduce methods in a class or structure to hide attributes, fields, and types with the same name in the base class. It also hides all base class methods with the same signature.

3. The introduced class or structure indexer will hide all base class indexers with the same name.

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.