Why should I avoid using Properties in C

0 votes

Jeffrey Richter, in his excellent book CLR Via C#, stated that he dislikes properties and advises against using them. He gave a justification, but I'm not sure I get it. Can somebody tell me why I should use properties and why I shouldn't? Does this change in C# 3.0 with automated properties?

I included Jeffrey Richter's thoughts as a reference:

• Field access is always accessible and writable, regardless of whether a property is read-only or write-only. It's recommended to provide both get and set accessor methods when defining a property.

• While a property method may throw an exception, field access does not.

• A field can be supplied as an out or ref parameter to a method, but not a property. The following code, for example, will not compile:

using System;
public sealed class SomeType
{
   private static String Name 
   {
     get { return null; }
     set {}
   }
   static void MethodWithOutParam(out String n) { n = null; }
   public static void Main()
   {
      // For the line of code below, the C# compiler emits the following:
      // error CS0206: A property or indexer may not
      // be passed as an out or ref parameter
      MethodWithOutParam(out Name);
   }
}

• While a property method can take a long time to perform, field access is always instantaneous. A common purpose for using properties is to execute thread synchronisation, which can cause the thread to hang indefinitely; consequently, if thread synchronisation is required, a property should not be used. In this case, a method is preferable. Calling the property method will be very slow if your class can be accessed remotely (for example, if your class is derived from System.MashalByRefObject), hence a method is preferable over a property. Classes derived from MarshalByRefObject, in my opinion, should never use properties.

• If a property method is called numerous times in a row, it may return a different value each time; a field always returns the same value. The System is in place. The readonly Now field of the DateTime class returns the current date and time. This property will return a different value each time you query it. This is an error, and Microsoft thinks they could correct it by making Now a method rather than a property in the class.

• A property method may have visible side effects, whereas field access does not. In other words, a user of a type should be able to change the order in which certain attributes described by the type are changed without causing the type to behave differently.

• A property method may use more memory or return a reference to something that isn't part of the object's state, thus changing the returned object has no effect on the original; querying a field always returns a reference to an object that is guaranteed to be part of the original object's state. Working with a property that returns a copy can be perplexing for developers, and this feature is often overlooked.

Jun 7, 2022 in C# by pranav
• 2,590 points
656 views

1 answer to this question.

0 votes
Jeff dislikes properties because they resemble fields, and developers who don't know the difference will treat them as such, expecting that they'll be inexpensive to execute and so on.

Personally, I disagree with him on this issue; I believe that properties make client code easier to read than similar method calls. I agree that developers should be aware that properties are essentially methods in disguise; nonetheless, I believe that training developers on this is preferable to making code more difficult to read by utilising methods. (I know that the corresponding C# code would be a lot easier to follow after seeing Java code with many getters and setters being called in the same statement.) The Law of Demeter is great in theory, but it doesn't always work out. Name. The word "length" is a good one to utilise.

(And no, automatically implemented properties have no bearing on this.)

This is similar to the arguments against utilising extension techniques - I get the logic, but in my opinion, the practical benefit (when used sparingly) exceeds the disadvantage.
answered Jun 7, 2022 by rajiv
• 1,620 points

Related Questions In C#

0 votes
1 answer

Which version of C# am I using

From developer cmd, type this csc -langversion:? This will ...READ MORE

answered Jun 7, 2022 in C# by pranav
• 2,590 points
481 views
0 votes
1 answer

How do I turn a C# object into a JSON string in .NET

Newtonsoft.json can be used to accomplish this. ...READ MORE

answered Jun 13, 2022 in C# by krishna
• 2,820 points
715 views
0 votes
1 answer

What does the extension method do in c# and why do you need it

Extension Methods allow you to add additional ...READ MORE

answered Jun 21, 2022 in C# by krishna
• 2,820 points
650 views
0 votes
0 answers

Using Stopwatch in C#

Is there a way to incorporate a ...READ MORE

Jun 11, 2022 in C# by krishna
• 2,820 points
398 views
0 votes
1 answer

What is the difference between a field and a property

The internal workings of a class should ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
638 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
5,716 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,110 views
+6 votes
16 answers

How do backend of these really cool games work?

Most of the games these days don't ...READ MORE

answered Jul 19, 2018 in Career Counselling by Kalgi
• 52,350 points
11,296 views
0 votes
1 answer

How can I compile and run c# program without using visual studio

If you have.NET Framework v4 installed, then C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe ...READ MORE

answered Jun 9, 2022 in C# by rajiv
• 1,620 points
1,298 views
0 votes
1 answer

Why program working differently on every run when using async?

You can use the async Main method ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
563 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP