Thursday, February 21, 2008

Property , C#

Use of Properties
In a way exposing the data members in a class as 'public' serves the same purpose as having the same member exposed via Property with simple get/set definitions.However the former is against the concept of Data Encapsulation , which states that the your type's fields should never be public exposed.


Usual Practice: To have a public 'get' and a protected 'set' accessor. Property in this case is listed with least restrictive accessibility ( I.e public in this example)

GetSetMethod Vs Property : is the same thing , just that with the method the code does not look that pretty.Properties offers more simplified syntax.

Backing Field: is the underlying private member access-wrapped in a property.

Code Inlining : At compile time the code for simple get set methods is Inlined by the compiler , in the method the property is accessed.This is only true for Release build and not Debug.
Assign a custom name to a Parameterful Properties ( Indexer) : System.Runtime.CompilerServices.IndexerNamee.g [lndexerName("Update")]

Still this cannot be used in C# to overload Indexers by Name ,we can only overload by parameter type.


System.Reflection.PropertyInfo : used to read the Property at runtime.

Deceptive:
- property may return data that's not part of its state.
- Confusing to tell by looking at the code if the property is full read-only or write only.

No comments: