Then, the attribute can be changed to the new Keyword used in method-C # To hide the member.

Source: Internet
Author: User

My god yesterday !! The property can be changed to a method !! As mentioned in, a class inherits the attributes of a class and can use the new method to overwrite this attribute, which makes me surprised. Some people in the reply understand this phenomenon in this way, and attributes are also translated into methods in the intermediate language.

I re-wrote a class

Public class ClassA
{
Private string a = "ClassA ";
// Attributes
Public string
{
Get
{
Return;
}
Set
{
A = value;
}
}

// Field
Public string B = "ClassA B ";

// Method
Public string C ()
{
Return "ClassA C ";
}

// Overload method
Public string D ()
{
Return "ClassA D ";
}

Public string D (string arr)
{
Return "Hello" + arr;
}
}

We use ildasm to open the generated file,

Indeed, the property accessors of a are compiled into the Set_A and Get_A methods. It seems that we have found the answer to this question.

However, the following code overwrites this theory.

Public class ClassB: ClassA
{
// A is an attribute. I hide it by using A method.
New public string ()
{
Return "ClassB ";
}

// B is a field. I hide it by using the method.
New public string B ()
{
Return "ClassB B ";
}

// C is a method. I used a field to hide it.
New public string C = "ClassB C ";

// D is an overloaded method. I used a field to hide it.
New public string D = "ClassB D ";
}

Do you think this code can be compiled, or will there be a warning during compilation ?? The answer is that the Code has been compiled and there are no error messages.

Conclusion: attributes and fields and methods (regardless of returned values) can be hidden using any attribute or field or method with the same name. The methods with parameters can be hidden using the same parameter signature method. Only one field or attribute can be hidden for a batch of overloaded methods.

PS: If you replace the members in Class B with private or protected, overwriting will not work. You need to access the parent class members during access.

Finally, it is very complicated to hide the new Keyword. We recommend that you use members of the same type to hide attributes and methods to hide attributes, so as to avoid unnecessary programming troubles.

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.