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 }}