1

I understand the diff. between both. and read the threads related to this. My question is regarding any performance gain. I used to create Properties with a local variable. and whenever i use the property INSIDE the class i use the local variable instead of property. I assumed there is a little gain in this rather than calling the property and then the property calling the local variable. In Automatic property its not possible. Is my assumption correct? does it have any gain (may be little) in my method?

sample

Public class class1
{
 private int _someField;
 public int SomeField
 {
  get{return _someField;}
  set {_someField = value;} 
 }

 Public void Insert()
 {
     str= "insert into table values(" + SomeField + ")  
     //or is it better to use like this?
    str= "insert into table values(" + _someField + ")

 }  
}
sjd
  • 1,329
  • 4
  • 28
  • 48
  • 1
    The performance gained is not very much. However, `Property` is a wrapper of code which can contain more code than just `get { return ...;}` and `set { ... = value;}`, such as for firing some related event. – King King Sep 02 '13 at 05:14
  • Yes I use property. the question is not about Property vs variable. its about property using a local variable and not using – sjd Sep 02 '13 at 05:17
  • @KingKing - Well, an auto property (`Property { get; set; }`) won't have more logic. - To the OP, that is something you could profile very easily on your own. But suffice it to say, if *that's* your bottleneck, then you might have other problems... – Corak Sep 02 '13 at 05:18
  • 1
    possible duplicate of [c#: why have empty get set properties instead of using a public member variable?](http://stackoverflow.com/questions/1876197/c-why-have-empty-get-set-properties-instead-of-using-a-public-member-variable) – H H Sep 02 '13 at 05:21
  • @Corak I talked about `manual Property`, I'm not a newbie not to know about what `Auto Property` is. – King King Sep 02 '13 at 05:26
  • I have added a sample to make the question more clear – sjd Sep 02 '13 at 05:28
  • @KingKing - did not mean to offend. OP was specifically talking about auto properties. – Corak Sep 02 '13 at 05:30
  • dont think its a duplicate.. :( – sjd Sep 02 '13 at 05:41

2 Answers2

5

Whether you use an automatic property that the compiler turns into a method call.. or you directly access a backing field.. it is likely that the JIT compiler will inline the field access anyway.

EDIT:

Automatic properties are compiled into method calls. So this:

public string Property { get; set; }

..becomes:

private string _property;

public string get_Property() {
    return _property;
}

public void set_Property(string value) {
    _property = value;
}

When the JIT compiler sees this, it is likely to inline the field access (it is a prime candidate really). So therefore, if you do this:

Property = "some value";

It won't generate this:

set_Property("some value");

It is more likely to do this:

_property = "some value";

So, really, there is no penalty at all. It is important to note that this is implementation specific (specific to the JIT compiler implementation).. but honestly, if that sort of thing isn't a candidate for inlining I don't know what is!

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • sorry i didnt get it correct. could you explain it a little. btw i use .net – sjd Sep 02 '13 at 05:22
  • I understand your question.. and this directly answers it. I would suggest clicking on the link I provided to JIT compilation and looking up how your .NET code becomes machine code. The answer is basically, "there is no performance difference". – Simon Whitehead Sep 02 '13 at 05:31
  • ok. so if call get_Property(somefield) it ll identify it is an automatic property and will read the backing field directly? – sjd Sep 02 '13 at 05:35
  • No. You call `Property`. The C# compiler generates a `get_Property()` method which it then calls. Then, the JIT compiler will _likely_ just access `_property` directly instead of calling `get_Property()`. – Simon Whitehead Sep 02 '13 at 05:38
2

When you haven't created a field yourself (Automatic property), compiler will generate the backing field for you. So, there is no performance gain at all.

This is what MSDN says

In C# 3.0 and later, 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. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

So,

private int _someField;
public int SomeField
{
    get{return _someField;}
    set {_someField = value;} 
}

is equivalent to

public int SomeField {get;set;}

After the Comment from Corak, i would like to add that if your property doesn't have any more logic then simple assignment (as in the above example), then

someClass._someField

will be same (well, almost)

someClass.SomeField

And, if you think that is your bottle neck. then think again. It can't be.

Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • The question is, if inside the class it is faster to use `_someField` instead of `SomeField`. – Corak Sep 02 '13 at 05:19
  • Of course it's faster to use `_someField` instead of `SomeField`, however it does not really matter. We have to choose between `_someField` or `SomeField` depending on other things than performance. – King King Sep 02 '13 at 05:35
  • @KingKing I like how firmly you make that statement, some of us won't make some important design choices only because It doesn't offer "pure" performance, but it's unavoidable to have performance penalties due to some abstractions In modern OO languages... You can get away with it being smart with modern C++ and probably other precompiled languages I do not know, but that's another story... – NicoBerrogorry Sep 29 '18 at 02:43