C # weird attributes (I)

Source: Internet
Author: User
Tags reflector

It is stated in advance that the code in this article is a test demonstration under Visual Studio 2008 and. NET Reflector 7.0.0.420. If the code is not tested under Visual Studio 2010, you have time to complete the test.
First, you need to explain what is an attribute.
Here is an explanation from msdn:
An attribute is a member that provides a flexible mechanism to read, write, or calculate the value of a private field. Attributes can be used like public data members, but they are actually special methods called "accessors. This makes it easy to access data, and also helps improve the security and flexibility of the method.
Start with the following instance.
There are the following Class:
Class
{
Public void Display ()
{
Console. Write ("aa ");
}
}
Then there is Class B. Note that no new is added before Displsy, as shown below:
Class B:
{
Public void Display ()
{
Console. Write ("bb ");
}
}
The client code is as follows:
Class Program
{
Static void Main (string [] args)
{
B B = new B ();
B. Display ();
Console. ReadKey ();
}
}
The output must be bb. I will not explain it!
If we only talk about it here, what are bricks, watermelon knives, and so on!
Don't worry, I won't let you watch it for so long!
Add a property Aa to a. the code is as follows:
Class
{
Public void Display ()
{
Console. Write ("aa ");
}
Private string aa = "aa ";
Public string Aa
{
Get {return aa ;}
Set {aa = value ;}
}
}
Very simple Class! Please let us know what A is like. NET Reflector!
Look!



The Aa property generates two methods in Class A: set_Aa (String) and get_Aa (). If you are a cainiao, it is necessary to find out why it is String rather than string here. It can help you understand the difference between string and String in C #, which will be general. net interview!
If you think that the old man only writes this article and stops, you will not understand the old man!
It's just the beginning of a good show. I want to make those kids shoes that are actually cainiao-level but self-recognized masters feel that they know little about it. I have written code for a year or two, however, there is a rare hacker in the world!
Good! Now let's take A look at this class. In Class A, it is actually like this!
A private variable named aa. The two methods are set_Aa (String) and get_Aa (). No problem. (hey, let's have a sneer here ), if you try to add these two methods to Class A in VS08, an error will occur!
Like this!
Class
{
Public void Display ()
{
Console. Write ("aa ");
}
Private string aa = "aa ";
Public string Aa
{
Get {return aa ;}
Set {aa = value ;}
}
Public void set_Aa (string)
{
Aa =;
}
Public string get_Aa ()
{
Return aa;
}
}
Vs tells you that you have made the following mistake:



To avoid such errors, we recommend that you check the code specification of C #. It is best not to use functions such as set_XXX (type t) and get_XXX (). If you have such requirements, we recommend that you use attributes!
......
One day in a month, some people think class B is not powerful and can be modified. However, class B is written like this!
Class B:
{
Public void Display ()
{
Console. Write ("bb ");
}

Public string bb;
Public void set_Aa (string)
{
Bb =;
}
Public string get_Aa ()
{
Return bb;
}
}
Are you familiar with this! Why is there two methods in Class B, set_Aa (String) and get_Aa (), and I feel so strange!
Okay, the client code is coming!
As follows:
Class Program
{
Static void Main (string [] args)
{
B B = new B ();
B. Aa = "bb ";
Console. WriteLine (B. bb );
Console. WriteLine (B. Aa );

Console. ReadKey ();
}
}
Tell me what the running result is with your understanding of OO?
If this is the result of your calculation, I think you have not been fooled by me. If not, haha, you have been fooled by me!



Why is no B. bb output? If this is not the result, please leave a message to inform the old man!
If you think you should add new before the two functions, you know the keywords, but you don't want to compile them. If you don't believe it, you can try it!
Okay! Are you wondering if Class B methods are not executed, right! You don't have to worry, that's it, but why?
........
If you find it strange, if A is like this:
Class
{
Public void Display ()
{
Console. Write ("aa ");
}
Private string aa = "aa ";
Public string Aa
{
Get {return aa ;}
Set {aa = value ;}
}
Public void set_Bb (string)
{
Aa =;
}
Public string get_Bb ()
{
Return aa;
}
}
B:
Class B:
{
Public void Display ()
{
Console. Write ("bb ");
}

Public string bb;
Public string Bb
{
Get {return bb ;}
Set {bb = value ;}
}
Public void set_Aa (string)
{
Bb =;
}
Public string get_Aa ()
{
Return bb;
}
}
The client is like this:
Class Program
{
Static void Main (string [] args)
{
B B = new B ();
B. Aa = "set aa ";
B. Bb = "set bb ";
Console. WriteLine (B. bb );
Console. WriteLine (B. Aa );
Console. ReadKey ();
}
}
What is the result?
The result is as follows:

 

Author: "Old Man Loves encapsulation"
 

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.