Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Property and Method [duplicate]

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 ?

like image 415
Asim Sajjad Avatar asked Mar 30 '10 11:03

Asim Sajjad


People also ask

What is the difference between method and property?

In most cases, methods are actions and properties are qualities. Using a method causes something to happen to an object, while using a property returns information about the object or causes a quality about the object to change.

What is the difference between properties and methods in JavaScript?

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method.

What is property and method in oops?

Method is a named action which can be applied to the object. Property is a named value, which the object has. For example, object Human has the property 'Age'. function is a more general thing, than a method. It is just an action, that doesn't belong to any object.

What is the difference between function and method in Visual Basic?

Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program. This eliminates the need for writing the same code again and again.


2 Answers

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?

like image 138
Marc Gravell Avatar answered Sep 19 '22 13:09

Marc Gravell


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.

like image 26
Oded Avatar answered Sep 21 '22 13:09

Oded