1

Many code samples in C# contain get and set code blocks. What is the purpose of them? I copy these blocks whey they appear in sample code, but have no idea what it is used for. Can someone please explain this to me?

Sam
  • 7,252
  • 16
  • 46
  • 65
Rick Bailly
  • 219
  • 2
  • 6
  • 1
    Properties. http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx – Jeroen Vannevel Sep 23 '13 at 18:19
  • Read about Object Oriented Programming and [Encapsulation](http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)) – Habib Sep 23 '13 at 18:19
  • Have you tried this: http://www.google.com/search?q=.net+c%23+get+set ? – Olexander Ivanitskyi Sep 23 '13 at 18:20
  • possible duplicate of [c#: getter/setter](http://stackoverflow.com/questions/6709072/c-getter-setter) – Travis J Sep 23 '13 at 18:21
  • 3
    Interesting.. will this question hit -100? – Sergey Berezovskiy Sep 23 '13 at 18:22
  • 1
    @lazyberezovsky: I hope not. That would give it a lower score than a "you should be able to buy rep with money" FR on meta. – ThiefMaster Sep 23 '13 at 18:23
  • 4
    I don't think that it's a particularly hard question, but that does not justify the number of downvotes it has received. I edited the text to add a little more clarity, and voted to reopen the question. – Sergey Kalinichenko Sep 23 '13 at 18:35
  • 1
    I dont understand why this has been down voted. The question is clear. – rollsch Feb 12 '17 at 10:56
  • 1
    Can someone explain how this can be downvoted 5 times, and closed as "unclear what you are asking" when it has multiple answers all very clearly aimed at the question, and having 8 upvotes? If it's unclear what is being asked, how can it have multiple very clear answers? Having answers to a question (and upvotes on those answers) validate the value of the question. – KWallace Oct 10 '17 at 17:29

5 Answers5

7

Getters and setters enable you to combine a pair of functions into one property, and let you use a syntax that looks like a member access expression or an assignment in place of syntax that looks like an explicit function call.

Here is a small example: instead of this

internal class Example 
{
    private int x;

    public int GetX() => x;
    public void SetX(int value) => x = value;
}

...

var e = new Example();

e.SetX(123);
Console.WriteLine($"X = {e.GetX()}");

They let you do this:

internal class Example 
{
    public int X { get; set; }
}

...

var e = new Example();
e.X = 123;
Console.WriteLine($"X = {e.GetX()}");

The syntax of the second code snippet is easier to read, because X looks like a variable. At the same time, the second snippet provides the same level of encapsulation, letting you hide the implementation behind the property.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
6

Do you mean this?:

public int SomeValue { get; set; }

This is basically syntactic shorthand for this:

private int someValue;
public int SomeValue
{
    get { return someValue; }
    set { someValue = value; }
}

Which itself is basically shorthand for this:

private int someValue;
public int GetSomeValue() { return someValue; }
public void SetSomeValue(int value) { someValue = value; }

(Though the compiler uses different conventions for the names of things when it does this.)

As it relates to OOP, this is the encapsulation of data. The idea is that objects should hide their data and expose functionality, instead of just exposing the data directly. So you don't necessarily modify someValue directly from outside the object. You call a method on the object and supply it with a value. Internally the object handles the actual storage of its data.

David
  • 208,112
  • 36
  • 198
  • 279
2
public int foo { get; set; }

This defines a property. It's basically like a public field but when it comes to reflection it's different. In C#/.NET it's common to use properties for public things. You can compare it with getter/setter methods in Java.

The awesome thing now is, that you can also use custom get/set code or make set less visible than get. That allows you to have the advantages of getter/setter methods without the ugliness of method calls instead of property accesses.

public int foo {
    get { return this.some_foo; }
    set { this.some_foo = value; this.run_code_after_change(); }
};
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

In english they are setters and getters.

Hope you teach yourself encapsulation, setters and getters were rised due to encapsulation.

Again in plain english, setters and getters give you the ability to access the property you define.

DarthVader
  • 52,984
  • 76
  • 209
  • 300
0

get and set are kind of syntactic sugar. It is the more readable way of implementing functions getting no params where you can insert for example validation (in setter) or calculations on fields in getters. Parentheses are useless in function like that.

pt12lol
  • 2,332
  • 1
  • 22
  • 48