Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of Property Get v Method

Tags:

c#

oop

I have a class with a collection of methods in, and I was wondering if there is any performance bonus of using methods over properties?

Some of the methods are fairly complex, but basically return an IEnumerable collection of objects, while others are a simple return values.Where(x => x.property == "comparison") Linq query.

Examples

Method:

public IEnumerable<PenDataRow> ActivePens() => Pens.Where(x => x.Status == "Active");

Property:

public IEnumerable<PenDataRow> ActivePens => Pens.Where(x => x.Status == "Active");

Would it be better to mark them as properties or methods?

like image 462
Tom Avatar asked Sep 04 '12 15:09

Tom


1 Answers

Properties are implemented as methods under the hood, so there is absolutely no performance difference. The guidelines for choosing between a property and a method are semantic.

like image 188
Jon Avatar answered Oct 06 '22 23:10

Jon