c# what is the point of using SET? – Education Career Blog

why do we do this:

 private string StatusText
 {
    set { toolStripStatusLabel1.Text = value; }
 }

instead of just this?

private string StatusText
{
   toolStripStatusLabel1.Text = value; 
}

i do not understand the point of using set?

,

These are two completely different things.

This is a method:

private string StatusText()
{
    toolStripStatusLabel1.Text = value; 
}

which is called like this:

StatusText();

(and which will not compile, because the local variable value cannot be found). To make it work, you would need to write it like this:

private string StatusText(string value)
{
    toolStripStatusLabel1.Text = value; 
}

and call it like this:

StatusText("bla");

On the other hand, this is the definition of a property:

private string StatusText
{
    set { toolStripStatusLabel1.Text = value; }
}

whose setter (hence the keyword set) is called like this:

StatusText = "bla";

,

Because there could also be a get:

get { return toolStripStatusLabel1.Text; }

Properties are syntactic sugar. When compiled you will have two methods get_property name and set_property name. If you only have the set method, only the set_propety name will be in the IL.

,

In a little more detail, since the OP has said she doesn’t understand gets and sets:

The get and set keywords are used to define a “property”. A property is a pair of methods – a “getter” and “setter” – that are used behind the scenes when the property is used or written to by other code. The advantage of a property over explicitly defining getter and setter methods is that you can use the property as if it were a “field” (a simple, publicly-visible member variable). The advantage of using a property instead of a field is that a property allows you to customize the behavior of assigning or using a variable. You can create “calculated fields” that are evaluated when needed based on other data in the object, or include basic validation or other business logic when reading or writing a value.

To define a property, you start by declaring it like you would a field, but then add a code block with get and set sub-blocks. You then define its read behavior in the get block, and the write behavior in the set block. You can choose to define only one accessor (making a “read-only” or “write-only” property), or you can define more limited visibility for one function or the other; for instance, you can make the getter public but the setter protected, so everyone can examine the property’s value but only the class’s other members and derived types can set its value.

The most common property implementation uses a “backing field”, a private variable that acts as the store for the value exposed by the property. To streamline this implementation, .NET 3.0 included the concept of an “auto-property”; if you didn’t define the code bodies of the getter and setter, the compiler would generate a basic implementation using a backing field.

,

First will not compile for value isn’t valid unless you have set.

private string StatusText
{
   toolStripStatusLabel1.Text = value; 
}

Check out MSDN on Accessors here.

Setting and Getting fields and properties…

Example:

private string statusText;
public string StatusText
{
  get { return this.statusText;}          
  set { this.statusText = value;
      toolStripStatusLabel1.Text = this.statusText; 
  }
}

,

private String StatusText
{
  get { ... }
  set { ... }
}

The get/set tokens are to distinguish between the get and set accessors.

,

The latter wouldn’t compile. The set part shows that it’s the setter part of a property.

An alternative is to just write a method:

private void SetStatusText(string value)
{
    toolStripStatusLabel1.Text = value;
}

,

To enable you to apply more complex logic, when the need will arise.

Leave a Comment