16

Which one is better to use when it come to return value for example

public int EmployeeAge
{
    get{return intEmployeeAge};
}

And

public int EmployeeAge()
{
    return intEmployeeAge;
}

Which one is better and why? And what is best programming practice to use when we have secnario like above ?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Asim Sajjad
  • 2,526
  • 10
  • 36
  • 73

8 Answers8

36

Properties are a useful way of expressing a feature of an object, allowing get/set in a common way that can be used by APIs like data-binding, reflection and serialization. So for simple values of the object, properties are handy. Properties can't take arguments, should not have significant side-effects*, and should return quickly and repeatably. Also, there is no such thing as an "extension property" (to mirror an extension method) nor a generic property.

(*=lazy loading etc isn't uncommon, however)

Methods (C# doesn't have functions) are better for expressing things that either change the state, or which have an expectation of taking some time and not necessarily being reproducible. They don't tend to work in binding / serialization etc.

Note that properties are actually just a special way of writing methods. There is little functional difference. It is all about expressing intent. The one thing you don't want to expose, however, is fields (the actual intEmployeeAge instance variable).

So I would have:

public int EmployeeAge { get{return intEmployeeAge}; }

or just (if on the Employee object):

public int Age { get{return intEmployeeAge}; }

Of course... then the question becomes "in what unit?" I assume that is years?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • /agreed. In a simple way, properties are designed to expose private fields outside the class to ensure no breaking changes if the class architecture is revised. – JoeBilly Mar 30 '10 at 14:52
  • @JoeBilly - well, you can do a *bit* more than that without it getting too messy. But simple is as simple does, so they say. – Marc Gravell Mar 30 '10 at 15:19
  • Sure it was popularized. I mean you can do a lot of thing with a proeprty (good or bad) but it is an accessor first. – JoeBilly Mar 30 '10 at 16:59
  • coming from java, it seems like c# properties act like mutator and accessor methods. Is this a correct assumption? If so, is there a point of having methods in C# to get instance variables when properties do so? Please correct me if I'm wrong! – Ben Sewards Dec 06 '12 at 01:07
  • 2
    @Ben you are correct. And the `get` / `set` **are** methods. Behind the scenes, that is actually `get_Age` and `get_EmployeeAge` – Marc Gravell Dec 06 '12 at 12:05
9

If all you need to do is return a value, use a property.

If you need to do something before returning a value, use a function.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    But in case of property I can also add some processing what you call it "do something" before return value in property as well, in above case both are returning value. but which is better ? – Asim Sajjad Mar 30 '10 at 11:44
  • Yes, you can, but you need to think about _meaning_. Most programmers, when calling a property do not expect it to change object state, but when calling a function they do expect this. – Oded Mar 30 '10 at 12:03
5

Properties holds object data

Functions defines object behavior

Take a look at -> Property Usage Guidelines

Morten Anderson
  • 2,301
  • 15
  • 20
2

Which one is better and why? And what is best programming practice to use when we have secnario like above ?

I write in C# however I prefer to use Get/Set functions, for me it's just better way to express what I can get from object and how I can change it's state (and this methods are grouped by alphabet in Intelisense which is also nice). However, if team prefers other conventions it's not a problem but when I work on my own projects it's just easier to read API.

e.g

Obejct1 o = new Object1();
o.P1;
o.P2;
o.P3;

from looking to API you can't say what you change in a public API or what it a read only property, unless you use IDE that shows you a small icon showing actually what you can do.

Object1 o = new Object1();
o.GetP1();
o.SetP2();
o.SetP3();

One can easily find from API how data can be changed by type's clients.

Andrey Taptunov
  • 9,367
  • 5
  • 31
  • 44
1

I'm a little late to this party, but I'll just mention another surprising difference between a property and a parameterless "get" method. As @MarcGravell notes, lazy loading is a common pattern when using properties, but beware of the Heisenberg Watch Window gotcha!

Community
  • 1
  • 1
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
1

A method returns values after work is completed and a value is the result of the work being done. I don't think this is what you are doing.

A property (accessor) is meant for returning variables, which seems to be what you're trying to achieve:

As per MSDN:

The accessor of a property contains the executable statements associated with getting (reading or computing) or setting (writing) the property. The accessor declarations can contain a get accessor, a set accessor, or both. The declarations take the following forms:

public int EmployeeAge
{
    get;
    set;
}

Have a look here, as it gives a very good description on the uses of these.

Kyle Rosendo
  • 25,001
  • 7
  • 80
  • 118
1

Property is a way explore the internal data element of a class in a simple manner. We can implement a properties with the type-safe get and set method.Property is implicitly called using calling convention.Property works on compile and runtime.

Method is a block of code that contain a series of statements.Method is explicitly called. Methods works on runtime.

0

I think this has a lot to do with the culture you are programming in. As I see it, the C# / .NET culture would prefer to use a property for this case.

My advice: Try to be consistent with the main libraries you are using.

BUT: Be wary of using properties (or functions that serve the same purpose, as in your example above), as they are often a sign of bad design. You want to tell your objects to do stuff, as opposed to asking them for information. Don't be religious about this, though, just be aware of this as a code smell.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • Culture? didn't get your point, as In both cases it onlye return employee age. – Asim Sajjad Mar 30 '10 at 11:29
  • By *culture* I mean the body of code that comes with a language / platform, the community of developers using them and the libraries they create. – Daren Thomas Mar 30 '10 at 11:34
  • I don't understand your code smell reference. A complicated object might have many properties especially if it has multiple ancestors... – DiningPhilanderer Mar 30 '10 at 12:24
  • 1
    @Dining Philanderer: Sure it can. But be wary of such objects. Make sure you are certain that it needs to be what it is, since having lots of properties could be a sign of bad design. Properties tend to create dependencies and complicated if/else code. – Daren Thomas Mar 30 '10 at 13:12