C# · CodeProject · Dotnet

C# Properties and its hidden flaws

Hello Friends,

For sure, i am not going to discuss about properties or its internal structure. But, as jeffrey says in his book properties do have alot of problems/confusions around it compared to a field, if a developer is not aware of it.

Though, i would like to outline a few if in case your not able to refer to it:

  1. It might throw an exception if the Property caller didnt know about it, it could crash his application.
  2. You can not pass properties as ref or out params.
  3. DateTime.Now is a property and returns different values at different instances say an continuous array call.

One of such problem i encountered in my earlier fresher days that i was trying out a small code using properties and the compiler didn’t issue any warnings or errors. But at run time, code started to crash by throwing StackOverFlowException.

  1. private static Int32 MyValue
  2.     {
  3.          get { return MyValue; }
  4.          set { MyValue = MyValue; }
  5.     }

This small stupid mistakes goes unnoticed though while unit testing should have been caught. It had caught me confused for a while though.

Happy coding 🙂

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s