3

May be its a duplicate question. I did search about this and referred these Articles

The point i understood was,

  1. Access like making field read only
  2. We can include some logics in
  3. setter/getter used for data binding

What I really want to clear was,

public class Employee {
    public string strName;
}

public class Employee {
    public string strName {get;set;} 
}

My questions:

  1. Whats the difference between this two implementations
  2. Is there any place ( i mean actual scenario) where we can justify a need for auto implemented properties instead the first implementation as shown above as first.

UPDATE

I know its a duplicate question and i mentioned it. Please consider my 2nd point in questions i asked. what the answer precisely ? I couldn't understand that.

Whether its a good practice or what is the need if i do not have any logic to set that value ?

Fine thank you all for replying. I got it now clear. Since i am very new i cant grasp it. But now i got the point. Sorry for wasting all your time.

Community
  • 1
  • 1
shanmugharaj
  • 3,814
  • 7
  • 42
  • 70
  • 1
    Actually, the three points that you list in the beginning are three things that *cannot* be done with auto-implemented properties, or at least not in as flexible or complete a way as with fully written properties. – O. R. Mapper Jan 17 '15 at 14:37
  • 1
    the choice is auto or manually implemented properties, just exposing a member variable is wrong. Think about the consequences of having to put some logic around setting name in a mature code base. Oh and stop with the hungarian notation. – Tony Hopkinson Jan 17 '15 at 14:38
  • @O.R.Mapper Thanks for reply. Fine, then is there any place where we can justify the need for aip over the normal implementation as i asked in the last part – shanmugharaj Jan 17 '15 at 14:39
  • Here is a SO answer that is helpful and address your questions to some point: http://stackoverflow.com/a/5203341/2777098 – display name Jan 17 '15 at 14:39
  • 2
    Auto implementaion is syntactic sugar, just stops you having to write a getter and setter for each property, nothing else – Tony Hopkinson Jan 17 '15 at 14:40
  • @DaveHillier i already mentioned its a duplicate. i was asked for a scenario where i need to use auto implemeted properties instead the first implementation as shown in my question. I need that point to be clear. So why i asked this question – shanmugharaj Jan 17 '15 at 14:43
  • It is covered - it is to making your interfaces robust to change. Changing a field to a property is not backwards compatible. Properties are considered the best way to expose a public interface. – Dave Hillier Jan 17 '15 at 15:01

2 Answers2

5

With auto implemented properties you can do

public class Employee {
 public string StrName {get; private set;} 
}

And make an external read-only, but internal settable property. Which is something you can't do with your public variable

Peter M
  • 7,309
  • 3
  • 50
  • 91
  • Shouldn't public property start with a capital letter as per conventions? – Aniket Thakur Apr 09 '18 at 13:51
  • @AniketThakur Of course it should. I blame the younger me for that! – Peter M Apr 09 '18 at 15:25
  • 1
    I think it's the `private` in `{ get; private set; }` that's the secret sauce for me. That's precisely what IDE0032 was trying to get me not to do in this case. Now I can, with minimal code, set up a property that's settable within the class, but read-only for all practical purposes outside... – ruffin Sep 04 '18 at 22:00
2

Having a field inside a class is not a good idea. using properties allows you to encapsulate your data better. and when you just want to have a field accessible without any logic in you class then you can use automatic properties.

there are many scenarios that using a field inside your class makes things go wrong and harder as your software evolve.

for example: assume you have

public class C
{
    public int Value;
}

in your code base.

then suddenly you realize that Value can not be set to zero. Then you have to make Value private and provide a SetValue() and GetValue() method. This is easy. but wait, how you gonna handle all other codes that relies on Value now?

but think about this one

public class C
{
    public int Value { get; set; }
}

now it just needs a backing field like _value and implementing the setter and getter.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • 6
    Your example doesn't make much sense. In a case where you want to have a property that can be get/set publicly, your example does not give a reason why the auto-implemented getter/setter are better. If you originally wrote the code as a property and subsequently changed it to have an implemented getter/setter, it would still work – Aidy J Jul 06 '18 at 16:27