Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use get; set; in c#

Tags:

c#

properties

I'm failing to understand what the difference is between initializing a variable, getting its value like this:

 //define a local variable.
   int i;

   i= 0;
   Console.WriteLine(i);

and the get; set; usage:

public int i
{
   get;
   set;
}

i = 0;
Console.WriteLine(i);

I've read some articles, but don't understand when I would use it.

like image 247
The Muffin Man Avatar asked Mar 20 '11 01:03

The Muffin Man


People also ask

What is the use of get set?

The get method returns the value of the variable name . The set method assigns a value to the name variable. The value keyword represents the value we assign to the property. If you don't fully understand it, take a look at the example below.

Why do we use get and set properties in C#?

A get property accessor is used to return the property value, and a set property accessor is used to assign a new value. In C# 9 and later, an init property accessor is used to assign a new value only during object construction. These accessors can have different access levels.

Is Get set a method?

Getter and Setter are methods used to protect your data and make your code more secure. Getter returns the value (accessors), it returns the value of data type int, String, double, float, etc.

What is get used for in C#?

The get keyword defines an accessor method in a property or indexer that returns the property value or the indexer element. For more information, see Properties, Auto-Implemented Properties and Indexers.


3 Answers

Basically, in that case, there is no difference, of the (many) advantages of using a property is the ability to add Events to your property, like so:

  public delegate void ChangedEventHandler(object sender, EventArgs e);

  int m_i = 0;
  public int i 
  {
      get { return m_i; }
      set { m_i = value; iChanged(self, null); }
  }

  public ChangedEventHandler iChanged;

This allows for code to know when I has been changed (there might be some syntax errors, I haven't focused on C# in a while, but the idea is similar). This is extremely important in winforms, as this is the major way of knowing when a button (or similar) has been clicked.


Also, this allows for additional functionality in the setter of a property, e.g. checking if it is in a certain range, like this:

  int m_i = 0;
  public int i {

  get { return m_i; }
  set { if (value > 10) throw new Exception("I cannot be greater than 10!"); m_i = value; }
  }
like image 125
Richard J. Ross III Avatar answered Oct 03 '22 13:10

Richard J. Ross III


If you think you might need a more complicated getter or setter later, the automatic property syntax lets you upgrade without recompiling all callers. Moving from a field (member variable) to a property is a breaking change, however.

Eric Lippert addresses the topic. Twice.

like image 29
Ben Voigt Avatar answered Oct 03 '22 11:10

Ben Voigt


Lets forget properties for a second... The real question you're asking (and you might not even know it) is why do you need properties (or getters and setters in some other languages) in the first place?

It's to promote encapsulation.

Properties just offer a nicer syntax for getters and setters, aka accessors (and indeed, a property just wraps set() and get() methods under the hood).

In c# 3 the c# team came up with auto properties, because the vast number of properties don't do anything with the variables (no additional logic), so auto properties are short hand for that scenario.

like image 28
Giovanni Galbo Avatar answered Oct 03 '22 13:10

Giovanni Galbo