What is the purpose of get : set? [closed]

旧巷老猫 提交于 2020-01-30 10:36:58

问题


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?


回答1:


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

class Example {
    private int x;
    public int GetX() {return x;}
    public void SetX(int value) { x = value;}
}
Example e = new Example();
e.SetX(123);
Console.WriteLine("X = {0}", e.GetX());

they let you do this:

class Example {
    public int X {get;set;}
}
Example e = new Example();
e.X = 123;
Console.WriteLine("X = {0}", e.X);

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.




回答2:


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.




回答3:


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(); }
};



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/18966091/what-is-the-purpose-of-get-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!