Getter和.NET中二传手声明 [英] Getter and Setter declaration in .NET

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

问题描述

我想知道什么是那些声明getter和setter方法​​之间的差异。哪一个是好(为什么)。第一种可全自动由Visual Studio生成。怎么样的人?谢谢

1日

 字符串_myProperty {获得;组; }
 

第二

 字符串_myProperty;

公共myProperty的字符串
{
    {返回_myProperty; }
    集合{_myProperty =价值; }
}
 

3

 字符串_myProperty;

公共getMyProperty()
{
  返回this._myProperty;
}

公共setMyProperty(字符串值)
{
  this._myProperty =价值;
}
 

解决方案

属性用来封装一些数据。你可以使用一个简单的领域:

 公共字符串MyField的
 

不过,这一领域可以通过你的类之外的所有用户访问。人们可以插入非法值或更改你没有想到办法的价值。

通过使用属性,你可以封装您的数据的访问方式。 C#有一个很好的语法转场到属性:

  myProperty的字符串{获取;组; }
 

这就是所谓的自动实现的属性。当需要时,你可以扩大你的属性:

 字符串_myProperty;

公共myProperty的字符串
{
    {返回_myProperty; }
    集合{_myProperty =价值; }
}
 

现在,您可以添加code,用于验证的价值你的二传手

 集
{
    如果(string.IsNullOrWhiteSpace(值))
        抛出新ArgumentNullException();

    _myProperty =价值;
}
 

属性也可以有不同的访问的getter和setter方法​​:

 公共字符串{myProperty的获取;私定; }
 

这样,您创建一个属性,它可以被每个人读取,但只能由类本身进行修改。

您还可以添加一个完全自定义的实现你的的getter

 公共myProperty的字符串
{
    得到
    {
        返回DateTime.Now.Second.ToString();
    }
}
 

在C#编译您自动实现的属性,它生成的中间语言(IL)。在您的IL你会看到一个 get_MyProperty set_MyProperty 方法。它还创建了一个名为℃的后备字段; myProperty的>。k_BackingField (通常这将是在C#非法的名字,但在IL它是有效的,你不会得到任何冲突,这种方式生成的类型和你自己的code)之间。然而,你应该使用C#中的官员财产语法。这在C#中一个更好的体验(例如使用智能感知)。

按照惯例,你不应该为这需要很长的时间的操作使用属性。

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

1st

string _myProperty { get; set; }

2nd

string _myProperty;

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

3rd

string _myProperty;

public getMyProperty()
{
  return this._myProperty;
}

public 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.

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

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

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

    _myProperty = value;
}

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.

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

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

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.

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

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