Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties and auto-implementations

I'm using .NET 4.5 in a VSTO addin for outlook 2013. I'm having some trouble fully grasping properties and accessors. Auto-implemented accessors which I assume are when you just write get; set; rather than get { //code }, etc. are also giving me trouble. I have a dictionary I use internally in my class. Here is my code:

private Dictionary<string, string> clientDict { get; set; }
private Dictionary<string, string> clientHistoryDict { get; set; }

then later on:

clientDict = new Dictionary<string, string>();
clientHistoryDict = new Dictionary<string, string>();

I am using the same names as the properties in the code later on, within the same class.

I never actually write:

private Dictionary<string, string> _clientDict; // etc.

to create the variables I just was using the property directly.

I tried changing my code to do this, and I had some issues and realized my understanding of properties is a bit mixed up.

Here are a few questions I need clarified that I can't seem to find the correct answer to.

First, is there any reason to use a private property? My dictionaries are never accessed outside of the class or in any derived classes so is there a reason to even use properties? I don't use any special validation or anything in the setter or anything like that.

Second, when I tried to change my code to use variables and then access them via the properties like your typical property example would, I ran into problems. I found an example where the getter was set to return _clientDict, but the setter was just set; It gave me the error: that I must give set a body because it's not abstract or partial. Why would it not auto-implement the setter for me in this instance?

Last, when I call new on the properties in the same class that it is declared in, what is the difference between doing that with a property and a normal variable of the same type? Do properties differ at all from variables in that case? Is it bad practice to use properties this way when it should be accomplished with private variables?

These may be some misguided questions but I can't find any other place that has the information to help me understand these distinctions. I've been playing around with properties to try and figure all of this out but I could use so me assistance.

like image 592
ss7 Avatar asked May 25 '15 15:05

ss7


People also ask

What are auto-implemented properties?

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property.

What is the point of auto-implemented property in C#?

Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects.

Should I use fields or properties?

Generally, you should declare your fields as private, then use Properties to get and set their values. By this way you won't affect their values them directly. This is common case practice since having public members violates the Encapsulation concept in OOP.


2 Answers

First, is there any reason to use a private property?

Usually, no. Properties are great for encapsulation. One advantage (there are many more) of using a property is that it can do validations before assignment. When you have something private, you usually don't need to protect things from yourself. Also, properties have the advantage of setting different accessors (private, protected, etc), where fields do not.

Why would it not auto-implement the setter for me in this instance?

We have to understand that auto-implemented properties aren't black magic. The compiler will generate a private backing field for us, instead of providing one ourselfs. From his point of view, he sees that you have a getter that returns a private field, but the setter is automatic, thatusually would indicate some kind of logical error in your code. Why would you return one value but set a completely different one? When you create a property with a backing field, you have to provide both the getter and setters, those are the rules.

when I call new on the properties in the same class that it is declared in, what is the difference between doing that with a property and a normal variable of the same type?

Semantically, Nothing. new belongs to the type being constructed and will emit a constructor call. The difference is once the newly created object is assigned. A field will cause the compiler to emit a stfld opcode. For a property it'll emit a call to invoke the property setter. When you access a property, the compiler will end up calling get_YourPropertyName vs a ldfld on the field.

Is it bad practice to use properties this way when it should be accomplished with private variables?

I wouldn't call it bad practice, but I would find it a bit weird to have a private property.

For more insights on fields and properties, see What is the difference between a Field and a Property in C#?

like image 153
Yuval Itzchakov Avatar answered Oct 21 '22 19:10

Yuval Itzchakov


Is there any reason to use a private property?

No - that's the whole point of auto implementation. It saves you having to write all that extra code when all you want to do is get or set what's in the private member variable. .Net handles the creation of the shadowing private member variable behind the scenes.

When I tried to change my code to use variables and then access them via the properties like your typical property example would, I ran into problems. I found an example where the getter was set to return _clientDict, but the setter was just set; It gave me the error: that I must give set a body because it's not abstract or partial. Why would it not auto-implement the setter for me in this instance?

My understanding is that it's all or nothing with auto implementation. (Open to correction there though). That said I have seen code compile with the set block simply defined as set { }. Edit: Just to clarify the set { } block won't actually set the value, it essentially swallows the call and does nothing - it will compile though.

When I call new on the properties in the same class that it is declared in, what is the difference between doing that with a property and a normal variable of the same type? Do properties differ at all from variables in that case? Is it bad practice to use properties this way when it should be accomplished with private variables?

There is no real difference as far as I am aware. The exact same thing is happening, it's just that .Net is handling the plumbing for you.

like image 24
amcdermott Avatar answered Oct 21 '22 19:10

amcdermott