关于获取和设置的快速问题

发布于 2024-09-27 15:05:48 字数 120 浏览 0 评论 0原文

如果类中有以下代码,get和set方法是否与该变量关联?如何使用类的实例访问 get 和 set?

public string Something { get; set; }

If there is the following code in a class, are get and set methods associated to the variable? How can I access get and set with an instance of the class?

public string Something { get; set; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

时光倒影 2024-10-04 15:05:48

这本质上是一个编译器技巧。当您编译代码时,编译器将生成一个隐藏字段以及在 get 和 set 中返回和设置该字段所需的代码。

您将像访问任何其他属性一样访问此属性。 MyClass.Something =“bla”

This is essentially a compiler trick. When you compile the code the compiler will generate a hidden field and the necessary code to return and set the field in the get and set.

You would access this property just like you would access any other property. MyClass.Something = "bla".

面如桃花 2024-10-04 15:05:48

此语法随.Net Framework 3.5(自动属性)一起提供,

就像:

private string something;
public string Something
{
     get { return something; }
     set { something = value; }
}

要访问此变量(应该位于 MyClass 类中):

// GET
    MyClass myObj = new MyClass();
    string test = myObj.Something;
// SET
    myObj.Something = "blabla";

This syntax comes with .Net Framework 3.5 (automatic-property)

It's like :

private string something;
public string Something
{
     get { return something; }
     set { something = value; }
}

To access to this variable (supposed to be in a MyClass class) :

// GET
    MyClass myObj = new MyClass();
    string test = myObj.Something;
// SET
    myObj.Something = "blabla";
桃气十足 2024-10-04 15:05:48

这是一个自动属性,它在编译器中创建一个支持字段,您不需要为其编写代码。

获取:

var str = instance.Something;

设置:

instance.Something = "new value";

This is an auto-property, which creates a backing field in the compiler, which you don't need to write code for.

get:

var str = instance.Something;

set:

instance.Something = "new value";
小ぇ时光︴ 2024-10-04 15:05:48
  1. 这是编译器的一个新功能,
  2. 称为自动属性。
  3. 您不需要为自动属性定义后备存储,编译器会为您完成这项工作。
  4. 您无法在自动属性中注入自定义代码,您需要恢复到声明属性的正常 1.1 样式。
  5. 您可以像访问普通属性一样访问自动属性
  1. It's a new feature of compiler
  2. It's called Automatic Properties
  3. You don't need to define a backing store for automatic properties, the compiler does that job for you.
  4. You can not inject custom code in automatic properties, you need to revert back to normal 1.1 style of declaring properties.
  5. You can access automatic properties as you access normal properties
小忆控 2024-10-04 15:05:48

编译器会为您创建一个包含 getter 和 setter 方法 (*) 的支持变量,但您不会在标准代码中看到它们。您只需直接访问该属性即可。

myClass.Something = "blah"; // uses set
string myValue = myClass.Something; // uses get;

*这些方法是为属性创建的,而不是自动实现的或不是自动实现的。在自动实现的属性的情况下,编译器生成的支持变量将添加到混合中。

A backing variable complete with getter and setter methods(*) are created for you by the compiler, but you would not see them in your standard code. You would simply access the property directly.

myClass.Something = "blah"; // uses set
string myValue = myClass.Something; // uses get;

*These methods are created for properties rather they are auto-implemented or not. The compiler-generated backing variable is added to the mix in the case of an auto-implemented property.

凉墨 2024-10-04 15:05:48

这类似于下面的代码,但击键次数要少得多:-)

public string Something {
    get() {
        return _Something;
    }
    set(string value) {
        _Something = value;
    }
}

This is like the code below, but many fewer keystrokes :-)

public string Something {
    get() {
        return _Something;
    }
    set(string value) {
        _Something = value;
    }
}
懷念過去 2024-10-04 15:05:48

编译器将其转换

public string Something { get; set; } 

为这样的内容(在 IL 中,为了方便起见,将其转换为 C#):

string _something;
public string get_Something() { return _something; }
public void set_Something(string value) { _something = value; } 

另外,编译器将这些行转换为:

Something = "test";
var result = Something;

转换为:

set_Something("test");
var result = get_Something();

所以你看,下面都是方法调用(就像在 Java 中一样),但它是在 C# 中使用属性语法确实甜蜜。但是,如果您尝试直接调用这些方法,则会得到 错误

The compiler turns this:

public string Something { get; set; } 

Into something like this (in IL, converted to C# for your convenience):

string _something;
public string get_Something() { return _something; }
public void set_Something(string value) { _something = value; } 

Also, the compiler turns these lines:

Something = "test";
var result = Something;

Into this:

set_Something("test");
var result = get_Something();

So you see, it's all method calls underneath (just like in Java), but it's really sweet to have the property syntax in C#. But if you try to call these methods directly, you get an error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文