Confused about Properties in C#

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
So I have been playing around with C# for a little while now and have been reading several lessons and tutorials on the subject. What I'm confused about are properties. I don't see how they are different than public variables. Is is that you can execute some lines of code in the set{} and get{} blocks?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Properties aren't analogous to public variables. Better to think of them as shortcut methods that return a value.

To answer your direct question, yes, the primary benefit is that you can control get and set through the specified code blocks. Further you can provide only a get (read-only), and you can use access modifiers (as of C# 2.0) to, for example, have a public get and a protected set.

I used to be pretty much on the fence regarding properties, and they've come in for a lot of criticism. I think once you get used to the syntax they allow you to do some useful things. For example, if a particular value of a type is calculated at runtime from other values, perhaps by applying business logic, then I will place that calculation in the get{} block of the property. You can do the same thing with methods, but this way you provide a nice, easy to apply syntax for consumers.
 

stash

Diamond Member
Jun 22, 2000
5,468
0
0
They give you the encapsulation aspect of OOP with a less awkward syntax than standard accessor/mutator methods. So instead of writing car.setMake("BMW") you can write car.Make = "BMW". They're different from public variables in that they allow you to make your field (variable) private while allowing read and/or write access in a controlled manner.

Under the covers, properties are converted into accessor/mutator methods when compiled into CIL.

C# 3.0 includes a new automatic properties feature, where you don't need to explicitly define the field that the property encapsulates, but I think this is limited to standard get/set properties (no logic within the property), so that limits their usefulness.
 

jsedlak

Senior member
Mar 2, 2008
278
0
71
Also know that public fields cannot be declared in an interface so any higher level of Object Oriented Design will be impossible without properties.

And as the others say, you can do other things in the getter and setters since they are essentially methods.
 

Apathetic

Platinum Member
Dec 23, 2002
2,587
6
81
Being able to have a public get and a private set gets rid of my biggest complaint about C# properties.

Dave

Originally posted by: Markbnj
Properties aren't analogous to public variables. Better to think of them as shortcut methods that return a value.

To answer your direct question, yes, the primary benefit is that you can control get and set through the specified code blocks. Further you can provide only a get (read-only), and you can use access modifiers (as of C# 2.0) to, for example, have a public get and a protected set.

I used to be pretty much on the fence regarding properties, and they've come in for a lot of criticism. I think once you get used to the syntax they allow you to do some useful things. For example, if a particular value of a type is calculated at runtime from other values, perhaps by applying business logic, then I will place that calculation in the get{} block of the property. You can do the same thing with methods, but this way you provide a nice, easy to apply syntax for consumers.

 

jsedlak

Senior member
Mar 2, 2008
278
0
71
Properties can also be abstract!!!

public abstract class Foo{
public abstract int Value { get; set; }
}