1

I am writing getter setter in C#, I can write in both style or syntax, is their any specific scenario when to use one of them? Encapsulation is achieve from both of them?

Example 1:

public List<ChatMessage> MessageListWithoutPrivateVariable { get; set; }

Example 2:

public List<ChatMessage> MessageList {
  set {
    messageCollection = value;
  } get {
    return messageCollection;
  }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
bilal
  • 648
  • 8
  • 26
  • The second one is the same as what's achieved in the first implicitly; so no reason to use one over the other. If, on the other hand, something more interesting were going on in the get/setters then the difference, reasons, and benefits are obvious. – ChiefTwoPencils Nov 04 '16 at 05:42
  • 1
    The only difference between the two of them is that you can actually do some check ups in the second one before using the get or set method right inside the method itself. – Paul Karam Nov 04 '16 at 05:48
  • possible duplicate of - http://stackoverflow.com/questions/8116951/any-reason-to-use-auto-implemented-properties-over-manual-implemented-properties – RBT Nov 04 '16 at 05:59

5 Answers5

5

In general both ways do the same. Before C# v3.0, the first possibility wasn't there for programmers. But Microsoft thought it would be much more efficient if a programmer can write the shorthand style as you described first.

So in fact it's just the abbreviated form also known as auto-implemented properties. C# automatically generates a backing field in the background so encapsulation is granted.

In some scenarios you will need the second approach also known as manual properties. For example if you want to use INotifyPropertyChanged or some other value checks in the setter.

To answer your question: Use what you like. Or use first approach if possible and second if needed.

RBT
  • 24,161
  • 21
  • 159
  • 240
Sebi
  • 3,879
  • 2
  • 35
  • 62
0

As you can see if you run the code below, the compiler generates a private variable for the variable free version.

using System;
using System.Reflection;

namespace SO40415991
{
  class MyClass
  {
    public int MyFirstValue { get; set; }

    private int m_mySecondValue;
    public int MySecondValue
    {
      get { return m_mySecondValue; }
      set
      {
        m_mySecondValue = value;
      }
    }

  }


  class Program
  {
    static void Main(string[] args)
    {
      FieldInfo[] fis = typeof(MyClass).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

      foreach (var fi in fis)
      {
        Console.WriteLine($"{ fi.Name} {fi.IsPrivate}");
      }

      Console.WriteLine();
      Console.WriteLine("END");
      Console.ReadLine();
    }
  }
}

There is no need for an explicit private variable unless you want to use it internally in you class (a field is slightly faster to access than a property).

0

Example 1 is a shorthand syntax of Example 2 introduced in C# 3.0. If you code in C# 2.0, you should use Example 2 but after 3.0, you can use a shorthand version as in Example 1.

Anyways, you can use like Example 2 in/after 3.0 if you need to do some manipulation (or) calculations as below.

private List<ChatMessage> messageCollection;

public List<ChatMessage> MessageList {
  set {
    messageCollection = value;
  } get {
    if(messageCollection == null) {
      return new List<ChatMessage>();
    } else {
      return messageCollection;
    }
  }
}
Aruna
  • 11,959
  • 3
  • 28
  • 42
  • 2
    It seems like a bad idea to return a new list without assigning that to `messageCollection` first. Someone might call `obj.MessageList.Add(item)` and not get the result they expect. – Millie Smith Nov 04 '16 at 05:59
  • Ha ha, it's not an answer, it's to explain him where to use the full property instead of shorthand one :-) – Aruna Nov 04 '16 at 06:50
0

{ get; set; } is called "auto property", essentially a shorthand (of your "Example 2") when you write { get; set; }- similar code (as Example 2) will be generated by the compiler.

MushtaqAR
  • 71
  • 1
  • 5
0

Both are same but different.

as long as you are not providing extra definition, both representation behave same, like below

public int weight { get; set; }

private int _weight;
public int weight
{
   get { return _weight;}
   set { _weight = value;}
}

but,lets consider the other scenario, what if weight > 40 , you want to define something different. It can be either modify the backing field _weight or it may change the other private variable (_charge) as well.

  public int weight
    {
     get { return _weight; }
     set
     {
      _weight = value;
      if (_weight > 40)
          _charge = _weight * 2;
      else
         _charge = _weight * 1;
     }
  }

  private int _charge;
  public int Charge
  {
    get { return _charge; }
    set { _charge = value; }
  }

It makes a huge difference when you want to apply some business logic on your property just like above.

Ramakrishnan
  • 5,254
  • 9
  • 34
  • 41