About the use of new in C #

Source: Internet
Author: User

modifier : Hides the members in the base class (is a member of the base class, so fields, properties, events, and so on can be hidden, not just methods)

public class car{public    void Writename (string name)    {        Console.WriteLine (name);}    } public class newcar:car{public    void Writename (string name)    {        Console.WriteLine ("Car Name": +name);}    }

There is an error in this code: vs will Prompt

In order to enable subclasses to use Writename This method, we have to use the New keyword to hide the method in the parent class, or we have created a new Writename method, which is generally used when writing third-party plugins

public class newcar:car{    //Use New to hide methods in the base class public    new void Writename (string name)    {        Console.WriteLine (" Car name ": +name);}    }

Of course, if you want to use a member of the parent class, just show the transformation.


constraint : The new constraint specifies that any type parameter in a generic class declaration must have a public parameterless constructor. It is important to note that if you want to use the new constraint, the type cannot be an abstract type;

When there are multiple constraints in a generic class, the new constraint must be written in the last

public class cars<t>        where t:icomparable, new () {}

The basic usage of new is so much, and of course there are some other small points of knowledge, such as creating anonymous classes and so on.

About the use of new in C #

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.