New usage, new usage

Source: Internet
Author: User

New usage, new usage

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

New operator
Used to create objects and call constructors.

New Modifier
Used to hide an inherited member from a base class member.

New Constraint
It is used to restrict the types of parameters that may be used as type parameters in a generic declaration.

New modifier (C # reference)

When used as a modifier, The new Keyword can explicitly hide members inherited from the base class. Hiding inherited members means that the derived version of the member replaces the base class version. Hidden members are allowed without the new modifier, but warnings are generated. Explicitly hiding a member with new removes this warning and records the fact that the member is replaced with a derived version.

To hide an inherited Member, declare the member in the derived class with the same name and use the new modifier to modify the member.

New operator (C # reference)

Used to create objects and call constructors. For example:
Class1 o = new Class1 ();

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 constraint (C # reference)

The new constraint specifies that any type parameter in the generic class declaration must have a common non-parameter constructor. When a generic class creates a new instance of the type, apply this constraint to the type parameter, as shown in the following example:

Class ItemFactory <T> where T: new ()
{
Public T GetNewItem ()
{
Return new T ();
}
}

 

 

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.

4. It is incorrect to use both new and override on the same member.

Note: warnings are generated using the new modifier in the declaration that does not hide inherited members.

 

Example

In this example, the nested class MyClass hides the class with the same name in the base class. This example not only shows how to use a fully qualified name to access hidden class members, but also shows how to use the new modifier to remove warning messages.

1 using System;
2
3 public class MyBaseC
4
5 {
6
7. public class MyClass
8
9 {
10
11 public int x = 200;
12
13 public int y;
14
15}
16
17}
18
19
20
21 public class MyDerivedC: MyBaseC
22
23 {
24
25 new public class MyClass // nested type hiding the base type members
26
27 {
28
29 public int x = 100;
30
31 public int y;
32
33 public int z;
34
35}
36
37
38
39 public static void Main ()
40
41 {
42
43 // Creating object from the overlapping class:
44
45 MyClass S1 = new MyClass ();
46
47
48
49 // Creating object from the hidden class:
50
51 MyBaseC. MyClass S2 = new MyBaseC. MyClass ();
52
53
54
55 Console. WriteLine (S1.x );
56
57 Console. WriteLine (S2.x );
58
59}
60
61}
62
Output

100

200

 

Reference page: http://qingqingquege.cnblogs.com/p/5933752.html

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.