C # Hide the base class method and Partial in the class,

Source: Internet
Author: User

C # Hide the base class method and Partial in the class,

Today, for. NET developers, the most happy thing is that Microsoft is engaged in open source, which is a long face for. NET development. Although I am a beginner, this will undoubtedly give me great learning motivation. Fighting !!!

When a class inherits a member from the parent class, it inherits its Execution Code. If the Member is a virtual Member, we can use override to overwrite the Execution Code. But whether it is virtual or not, it can be hidden by the new keyword. For example:

Public class BaseClass {public void DoSomething () {// Execution Code} public class ChildClass: BaseClass {new public void DoSomething () {// hide the DoSomething method of the base class }}

If the new keyword is not used, the compiler will warn that the base class members are hidden. In this way, we can hide a method that we don't want to inherit when it runs like a base class.

There are two classes:

public class BaseClass{    public virtual void DoSomething()    {        Console.WriteLine("Base Function!");    }}public class ChildClass:BaseClass{    new public void DoSomething()    {        Console.WriteLine("Child Function!");    }}

We use the following code to call them.

ChildClass child= new ChildClass();BaseClass baseClass;baseClass = child;baseClass.DoSomething();

Result:

BaseFunction!

If we do not use the new Keyword, but override to override the base class method, the result is "Child Function !".

 

Partial definition Partial classification and Partial Method

We can use Partial to define a class. With this keyword, we can define fields, attributes, and constructors in a file, and define the methods in another file.

Public partial class MyClass {// definition of partial category}

The definition of partial classification is used in the windows Form application. The Windows Form code is in Form. cs and Form. Designer. cs respectively.

We can also define a method in different partial categories, declare it in one partial category, and implement it in another partial category, for example, the following code:

Public partial class MyClass {partial void MyPartialMethod ();} public partial class MyClass {partial void MyPartialMethod () {// method Implementation }}

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.