A detailed introduction to several uses of new in C #

Source: Internet
Author: User
This paper mainly introduces the use of New in C #, has a good reference value, following the small series to see it together

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

Used to constrain the type of a parameter that may be used as a type parameter in a generic declaration.

new modifier (C # Reference)

When used as a modifier, the new keyword can explicitly hide members that inherit from a base class. Hiding an inherited member means that the derived version of the member replaces the base class version. Hiding members without using the new modifier is allowed, but generates a warning. Using new to explicitly hide a member cancels this warning and records the fact that it 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 decorate the member

New operator (C # Reference)

1. Used to create objects and call constructors. For example:

Class1 o = new Class1 ();

2. Also used to call the default constructor for value types

Example: int myInt = new int ();

MyInt is initialized to 0, which is the default value of type int. The effect of the statement is equal to: int myInt = 0;

3. You cannot reload the new operator.

4. If the new operator fails to allocate memory, it throws a OutOfMemoryException exception

New constraint (C # Reference)

The new constraint specifies that any type parameter in a generic class declaration must have a public parameterless constructor. When a new instance of a type is created by a generic class, the constraint is applied 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, designations, attributes, or types in a class or struct to hide all base class members with the same name.

2. Introduce methods in a class or struct to hide properties, fields, and types that have the same name in the base class. It also hides all base class methods with the same signature.

3. Introducing an indexer in a class or struct hides all base class indexers with the same name.

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

Note: Using the new modifier in a declaration that does not hide an inherited member generates a warning.

Example

In this example, the nested class MyClass hides the class with the same name in the base class. This example demonstrates how to use a fully qualified name to access a hidden class member, as well as how to use the new modifier to dismiss a warning message.

Using System;    public class MyBaseC {public class MyClass {public int x = 200;    public int y;    }} public class Myderivedc:mybasec {new public class MyClass//nested type hiding the base type members    {public int x = 100;    public int y;    public int z;    public static void Main () {//Creating object from the overlapping class:myclass S1 = new MyClass ();    Creating object from the hidden class:MyBaseC.MyClass S2 = new Mybasec.myclass ();    Console.WriteLine (s1.x);    Console.WriteLine (s2.x); }} output 100 200
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.