Sunwen Tutorial----C # Advanced (vi)

Source: Internet
Author: User
Now what I'm going to say is version processing in C #. This is actually a problem that any software must take into account. There is more than one version of each software (except for what I write), so version processing is very important. Java handled the problem well, and I personally think that C # borrowed Java's approach, so Did a good job too.

In C #, if you use the keyword virtual when declaring a method, in a derived class, you can use the override or the new keyword to discard it or ignore it. If you used the keyword virtual in the parent class, Instead of using the override or New keyword in its derived class, and directly referencing a method with the same name, the compiler will error and will run in new mode, which ignores methods in the derived class. The following examples can help you understand:


From://Versioning\versioning.cs
001:public class MyBase
002: {
003:public virtual String Meth1 ()
004: {
005:return "Mybase-meth1";
006:}
007:public virtual String Meth2 ()
008: {
009:return "Mybase-meth2";
010:}
011:public virtual String Meth3 ()
012: {
013:return "Mybase-meth3";
014:}
015:}
016:
017:class Myderived:mybase
018: {
019:public override string Meth1 ()
020: {
021:return "Myderived-meth1";
022:}
023:public new String Meth2 ()
024: {
025:return "Myderived-meth2";
026:}
027:public string Meth3 ()//The system will have a warning here and will hide the method Meth3 ()
028:
029:
030: {
031:return "Myderived-meth3";
032:}
033:
034:public static void Main ()
035: {
036:myderived MD = new myderived ();
037:mybase MB = (MyBase) MD;
038:
039:system.console.writeline (Mb.meth1 ());
040:system.console.writeline (Mb.meth2 ());
041:system.console.writeline (Mb.meth3 ());
042:}
043:}

Output:

Myderived-meth1
Mybase-meth2
Mybase-meth3
It can be clearly seen that the output of the latter two new keywords is the output of the methods in the parent class, so it can be seen that the function of the new keyword is to use the previous method, rather than my current method, if this method is used in the previous version. The virtual method works just the opposite way. Its role is if there is such a method in the parent class, then use the method I am writing now, let the previous fuck off! However, it is not very good to use new, it is misleading (bad,!&*%$#@ to hit me again).

If you remove the No. 037 line, change the MB in 039-041 to MD, and the output turns to:

Myderived-meth1
Myderived-meth2
Myderived-meth3

What does this mean, that the override and the new keyword will only take effect when the object of the derived class is reshaped by the parent class. Hehe, this is really a bit difficult to understand, we only have to do their own work, to find out the organs, the so-called "practice is the only Test C # standard", haha!

In C #, you are free to add a method to a derived class, or to override a method of the parent class, as shown below, very simple:

Class Base {}
Class Derived:base
{
public void F () {}
}
And:

Class Base
{
public void F () {}
}
Class Derived:base
{
public void F () {}
}

The above is the Sunwen tutorial----C # Advanced (vi) of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.