More effective C # Chinese subtitle: 50 Concrete ways to improve C # programs [US] Bill Wagner, Chen Lifu, 1th edition of the People's post and Telecommunications press, January 2010
More effective C # Chinese, 3rd C # design practice, Entry 26: Ensure that properties behave like data
There are two sides to the property. From the outside it looks like a simple data element. Internally, it is implemented in the form of a method. This duality may allow you to create attributes that confuse the user. Developers using your type will assume that the properties are the same as accessing the data members directly. If your attribute does not conform to this assumption, then the user may have a misunderstanding. Attributes should feel like direct access to data members.
If you can correctly use attributes to describe data members, you can meet the expectations of the client developer. First, the client developer expects the get accessor of the two call property to be connected to the same result.
Somevalue = Someobject.importantproperty; Debug. Assert (somevalue = = Someobject.importantproperty);
Of course, this assumption may not be true in a multithreaded environment-whether using attributes or fields. However, in other cases, repeated access to the same property should be the same result.
Also, the type of consumer does not expect a property accessor to do much work. A getter that invokes a property should not affect the amount of time it takes. Similarly, the set accessor of a property may perform some validation, but it should not be too time-consuming.
This entry is very reasonable. However, there are exceptions: the DateTime.Now property. This very common attribute is obviously not satisfying: repeated access to the same property should be the same result. The now property of the DateTime class is static, so let's take a look at the Stopwatch.elapsed property:
:usingSystem;by :usingSystem.Diagnostics;The following:Note:Sealed classTest: {: static voidMain ():{: Console. WriteLine ("OS Version:"+Environment. OSVersion);: Console. WriteLine ("CLR Version:"+Environment. Version);Ten: Console. WriteLine ("Ishighresolution:"+Stopwatch. Ishighresolution);One : Console. WriteLine ("Frequency: {0:n0}",Stopwatch. Frequency);: varStopwatch =Stopwatch. StartNew ();: Console. WriteLine ("Elapsed:"+ Stopwatch. Elapsed);: Console. WriteLine ("Elapsed:"+ Stopwatch. Elapsed);:Stopwatch. Stop ();:}:}
The results of the program running on the Linux operating system are as follows:
OS Version:unix 2.6.32.24CLR version:2.0.50727.1433ishighresolution:truefrequency:10,000,000elapsed: 00:00:00.0000993elapsed:00:00:00.0013385
The results of the program running in the Windows operating system are as follows:
OS version:microsoft Windows NT 6.0.6002 Service Pack 2CLR version:4.0.30319.1ishighresolution:truefrequency:14,3 18,180elapsed:00:00:00.0000245elapsed:00:00:00.0260632
The Elapsed property of the Stopwatch class is also not satisfied: repeated access to the same property should be the same result.
The operating environment for the above program is as follows:
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
C #: Make sure that properties behave like data