-2

I am a bit confused with the get set property in C#.

I have the simple code below:

using System;

class Example
{
    int _number;
    public int Number
    {
        get
        {
            return this._number;
        }
        set
        {
            this._number = value;
        }

    }
}

class Program
{
    static void Main()
    {
        Example example = new Example();
        example.Number = 5; // set { }
        Console.WriteLine(example.Number); // get { }
    }
}

The code above using get set properties. However, if I delete the get set code like below code, the results stay the same.

using System;

class Example
{
    int _number;
    public int Number;
    {


    }
}

class Program
{
    static void Main()
    {
        Example example = new Example();
        example.Number = 5; // set { }
        Console.WriteLine(example.Number); // get { }
    }
}

My query is, what is the get set code used for? In the above program, the results are same. Can you give me some simple code which show the get set usage?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Raspi Surya
  • 315
  • 2
  • 11
  • 28
  • `get` and `set` where where you define your own function for a setter and getting. Of course if you make it do the same thing as default you will not see a difference. For example if you had `get { return this._number * 2;}` you would see `10` in the console. – Spencer Wieczorek Aug 19 '17 at 14:22
  • 1
    [Difference between property and field](https://stackoverflow.com/questions/653536/difference-between-property-and-field-in-c-sharp-3-0) – Steve Aug 19 '17 at 14:33

1 Answers1

1

In your code, Number is simply a public field, as evidenced by the semicolon (;) at the end.

public int Number;

It is not a property, you just have an empty set of brackets right underneath which led to your confusion. If you were to remove the ; then you would actually have a property that is missing it's get, and would not compile at all.

All properties need to have a getter (setters are optional). If you want to avoid writing them, you can use auto properties, which take care of the backing field without you having to get involved:

public int Number { get; set; } // No field required

Note: A common usage pattern you'll see involving auto properties is the following:

public int Number { get; private set; }

This allows for properties that can be read from anywhere, but can only be modified from within the class they belong to.

EDIT: To answer your question, the main difference between fields and properties is in encapsulation. You can read more about the general differences between fields and properties here.

However, the example you have given has one additional difference, the private set. A normal field can be written from and to throughout the program. A property with a private setter however can only be modified from inside the class it belongs to.

Example:

public class Foo
{
    public int Id { get; private set; }
    public string Name;

    public Foo()
    {
        this.Id = 1; // This works!
    }
}

Here, Name is a field and Id is a property with a private setter. Notice that we modify Id in the constructor and that works, because it is within the class Id belongs to. Moving outside the class however:

var foo = new Foo();

// Field (no get and set):
foo.Name = "test" // Works
string bar = foo.Name; // Works

// Property (get and *private* set)
int i = foo.Id; // Works, because get is public
foo.Id = 2; // Doesn't work, because set is private
stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • Hi, I am still bit confused about the properties and field. What is the difference between public int Number; and public int Number { get; private set; } ? both seems same. Do you have a simple code to show me the difference? – Raspi Surya Aug 19 '17 at 14:49
  • Hi, as I can see the code, `foo.Name = "test" <<---- this is same with get?` `string bar = foo.Name; // Works <<---- this is same with set?` as far as my observation, we can set and get the field without set `public string Name { get; private set; }` – Raspi Surya Aug 19 '17 at 15:04
  • @RaspiSurya Fields dont require `get` and `set`, only properties do. You can read and modify fields by default. Check out the code in the edit again, and read the post I have linked to. It will clear up some confusion – stelioslogothetis Aug 19 '17 at 15:07
  • May I conclude that instead of using the `public int Number;` , we can write `public int Number { get; private set; }` ? Because as I observed, if we write `public int Number { get; private set; }` ,it works as a field as well. – Raspi Surya Aug 19 '17 at 15:17
  • @RaspiSurya It does not. As you can see in the last line of the example, the property that has `private set` can *only* be written to from within the class it belongs to. The field however can also be written to from outside. If you make the `set` public (`public int Number { get; set; }`), *then* it works the same as a field. – stelioslogothetis Aug 19 '17 at 15:18