C # new Keyword,

Source: Internet
Author: User

C # new Keyword,

In C #, The new Keyword can be used as an operator, modifier, or constraint.

1. new OPERATOR: used to create objects and call constructors.
2. new modifier: when used as a modifier, The new Keyword can explicitly hide members inherited from the base class.
3. new constraint: used to restrict the types of parameters that may be used as type parameters in a generic declaration.

In our program, the new keyword is used without any time. Where can the new Keyword be used? Consider the following questions:

1. What is the difference between a new class object and a new struct or a new enum?

When a new class is created, the new class completes two tasks:First, call the new obj command to allocate memory to the instance in the managed heap.Second, call the constructor to initialize the object.

When a new struct is used, the new operator is used to call its constructor to complete instance initialization.

 

2. What can new do in addition to object creation in. NET?

New Keyword: as an operator, you can create objects and call constructors;

As a modifier, it can be used to hide the inherited members to the base class members and hide the virtual method of the base class in the derived class. It cannot coexist with override;

As a constraint, constraints in generic declaration can be used as parameter types of type parameters. That is, the new constraint specifies that all type parameters in a generic declaration must have a common no-argument constructor;

Use the new Keyword to implement polymorphism.

 

3. Can the new operator be reloaded?

The new operator cannot be overloaded.

 

4. What is the role of the new keyword in generics?

The new operator is used to return a reference pointing to the memory address of the managed heap allocated by the system. If the memory allocated by the new operator fails, an OutOfMemoryException is thrown.

 

5. What is the difference between a new inherited method and override?

The new method hides the base class method, and the override method overwrites the base class method. To access new hidden base class members, use the base keyword.

Similarities and differences between hiding and rewriting basic class methods

1: Method Rewriting: the method in the base class is identified by the virtual keyword, And Then override the class in the inheritance class.The methods in the base class have been rewritten and the functions have been lost.. When the reference of the base class object is directed to the object of the inherited class (polymorphism), this method is called to inherit the class method.

2: Method hiding: no matter whether the method in the base class uses the virtual keyword or not, the new Keyword can be used in the inherited class. (if the new keyword is not used, no errors will be generated, but a compilation warning will be generated) hide the methods in the base class. Hiding means hiding. Unlike rewriting,Rewriting means that the original (in the base class) does not exist, while hiding is the original. Therefore, when the reference of a base class object is directed to an object that inherits the class (polymorphism), calling this method is the method of the base class called.

Public class BaseClass {public void functionA () {Console. writeLine ("BaseFunctionA http://www.cnblogs.com/sosoft/");} public virtual void functionB () {Console. writeLine ("BaseFunctionB http://www.cnblogs.com/sosoft/");} public class DerivedClass: BaseClass {public new void functionA () {Console. writeLine ("DerivedFunctionA http://www.cnblogs.com/sosoft/");} public override void functionB () {Console. writeLine ("DerivedFunctionB http://www.cnblogs.com/sosoft ");}}

Call:
BaseClass baseFunction=new DerivedClass();baseFunction.functionA();baseFunction.functionB();

The result is:

BaseFunctionA http://www.cnblogs.com/sosoft/

DerivedFunctionB http://www.cnblogs.com/sosoft/

 

6. What are the differences between int I and int I = new int?

When a new int is used, the new operator is used to initialize the value of 0, so that the constructor can perform better initialization operations.


Hiding names by inheritance takes one of the following forms:

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

• 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.

• Introducing an indexer in a class or structure will hide all base-class indexers with the same name.

 

 

 

Q: What are the hidden application scenarios of new?

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.