.NET 中的 Getter 和 Setter 声明 [英] Getter and Setter declaration in .NET

查看:24
本文介绍了.NET 中的 Getter 和 Setter 声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 getter 和 setter 的声明之间有什么区别,以及是否有首选方法(以及为什么).第一个可以由 Visual Studio 自动生成.其他人呢?谢谢

I was wondering what were the differences between those declaration of getters and setters and if there is a preferred method (and why). The first one can be generated automaticly by Visual Studio. How about the others ? Thanks

第一个

string _myProperty { get; set; }

第二个

string _myProperty;

public string myProperty
{
    get { return _myProperty; }
    set { _myProperty = value; }
}

第三个

string _myProperty;

public string getMyProperty()
{
  return this._myProperty;
}

public void setMyProperty(string value)
{
  this._myProperty = value;
}

推荐答案

属性用于封装一些数据.您可以使用普通字段:

Properties are used to encapsulate some data. You could use a plain field:

public string MyField

但是您班级的所有外部用户都可以访问此字段.人们可以插入非法值或以您意想不到的方式更改值.

But this field can be accessed by all outside users of your class. People can insert illegal values or change the value in ways you didn't expect.

通过使用属性,您可以封装访问数据的方式.C# 有一个很好的语法将字段转换为属性:

By using a property, you can encapsulate the way your data is accessed. C# has a nice syntax for turning a field into a property:

string MyProperty { get; set; }

这称为自动实现的属性.当需要时,您可以将您的财产扩展到:

This is called an auto-implemented property. When the need arises you can expand your property to:

string _myProperty;

public string MyProperty
{
    get { return _myProperty; }
    set { _myProperty = value; }
}

现在您可以添加验证 setter 中的值的代码:

Now you can add code that validates the value in your setter:

set
{
    if (string.IsNullOrWhiteSpace(value))
        throw new ArgumentNullException();

    _myProperty = value;
}

属性也可以有不同的 getter 和 setter 访问器:

Properties can also have different accessors for the getter and the setter:

public string MyProperty { get; private set; }

通过这种方式,您可以创建一个所有人都可以读取但只能由类本身修改的属性.

This way you create a property that can be read by everyone but can only be modified by the class itself.

您还可以为您的 getter 添加完全自定义的实现:

You can also add a completely custom implementation for your getter:

public string MyProperty
{
    get
    {
        return DateTime.Now.Second.ToString();
    }
}

当 C# 编译您的自动实现的属性时,它会生成中间语言 (IL).在您的 IL 中,您将看到一个 get_MyPropertyset_MyProperty 方法.它还创建了一个名为 <MyProperty>k_BackingField 的支持字段(通常这在 C# 中是一个非法名称,但在 IL 中它是有效的.这样你就不会在生成的类型和你自己的类型之间产生任何冲突代码).但是,您应该使用 C# 中的官方属性语法.这在 C# 中创造了更好的体验(例如使用 IntelliSense).

When C# compiles your auto-implemented property, it generates Intermediate Language (IL). In your IL you will see a get_MyProperty and set_MyProperty method. It also creates a backing field called <MyProperty>k_BackingField (normally this would be an illegal name in C# but in IL it's valid. This way you won't get any conflicts between generated types and your own code). However, you should use the official property syntax in C#. This creates a nicer experience in C# (for example with IntelliSense).

按照惯例,您不应将属性用于需要很长时间的操作.

By convention, you shouldn't use properties for operations that take a long time.

这篇关于.NET 中的 Getter 和 Setter 声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆