C#,带变量和不带变量的属性之间的区别

发布于 2024-12-03 00:18:49 字数 572 浏览 1 评论 0原文

可能的重复:
封装 a 和私有成员作为属性并在没有私有成员的情况下定义属性?

我知道属性的基本功能。但当我深入查看文档时,我发现它们只是使用 get set 进行声明,而没有使用变量。

这两者之间有什么区别

public int EmpCode
{
    get { return _strEmpCode; }
    set { _strEmpCode = value; }
}  

public int EmpCode
{
    get; 
    set; 
}  

这是否只是随着 .net 框架升级而得到的一种更简单的编写方式。或者有什么功能上的区别吗?

Possible Duplicate:
What's the difference between encapsulating a private member as a property and defining a property without a private member?

I know the basic functionality of properties . But as i go through documentation in depth i see they are declared just with get set and without a variables .

what is the diffeence between these two

public int EmpCode
{
    get { return _strEmpCode; }
    set { _strEmpCode = value; }
}  

and

public int EmpCode
{
    get; 
    set; 
}  

Is it just a easier way of writing which got as .net frameworks got upgraded . Or is there any functional difference ?

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

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

发布评论

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

评论(6

我爱人 2024-12-10 00:18:49

后者称为自动属性,并且是相同的。它们是在 C#3 中引入的,您可以在这里阅读有关它们的更多信息:http://trashvin.blogspot.com/2008/05/automatic-properties-and-object.html

简单地说,自动属性是语法糖,因此开发人员只需键入更少的代码,编译器将为您生成私有字段以及公共 setter 和 getter。

The later is called an Automatic Property and is the same.They were introduced in C#3, you can read more about them here: http://trashvin.blogspot.com/2008/05/automatic-properties-and-object.html

Simply put, Automatic Properties are syntactic sugar so the developer has to type less code and the compiler will generate the private field and the public setter and getter for you.

御守 2024-12-10 00:18:49

这称为自动属性。没有功能上的区别。后一种语法只是前一种语法的简写。

C# 规范 的第 10.7.3 节提供了更多详细信息:

当某个属性被指定为自动实现的属性时,该属性将自动使用隐藏的支持字段,并且实现访问器以读取和写入该支持字段。

以下示例:

public class Point {
   public int X { get; set; } // automatically implemented
   public int Y { get; set; } // automatically implemented
}

相当于以下声明:

public class Point {
   private int x;
   private int y;
   public int X { get { return x; } set { x = value; } }
   public int Y { get { return y; } set { y = value; } }
}

This is called an automatic property. There is no functional difference. The latter syntax is just a shorthand of the former.

Section 10.7.3 of the C# specification gives more detail:

When a property is specified as an automatically implemented property, a hidden backing field is automatically available for the property, and the accessors are implemented to read from and write to that backing field.

The following example:

public class Point {
   public int X { get; set; } // automatically implemented
   public int Y { get; set; } // automatically implemented
}

is equivalent to the following declaration:

public class Point {
   private int x;
   private int y;
   public int X { get { return x; } set { x = value; } }
   public int Y { get { return y; } set { y = value; } }
}
梦明 2024-12-10 00:18:49

它称为自动属性,如果您不需要属性内部有任何逻辑,那么这是一种更简单的编写方法。编译时,编译器将自动为该属性生成一个支持变量,因此它是完全相同的。

稍后可以很容易地将属性更改为带有支持字段和一些逻辑的属性,并且这将不会破坏依赖于该属性的任何代码,如果您只是先使用公共字段,然后将其更改为属性,您将破坏依赖于该字段/属性的代码。

It's called Auto-properties and is just a easier way of writing them if you don't need any logic inside the property. When compiled, the compiler will auto-generate a backing variable for the property so it's exactly the same.

It's easy to change the property into a property with a backing field and some logic later on, and that will not break any code dependant on this property, If you instead just used a public field first, and then changed it into a property you would break the code that relied on this field/property.

浪漫人生路 2024-12-10 00:18:49

第二种方法实际上是自动属性,它隐式实现支持字段,这意味着您不能影响生成的字段名称。

大多数时候你并不关心,但是在使用序列化/反序列化在层之间传递对象的情况下,在某些情况下你会显式创建支持字段,以便摆脱 __propBackingField2735t34 像客户端上的名称。

此外,在显式编码的属性中,可以包含一些验证逻辑,但自动属性则不然

Second way is actually auto-property which implicitly implement backing field, which means that you cannot affect generated field name.

Most of the time you don't care, however in scenarios when you pass objects between layers using serializing / deserializing , you in some cases would have explicitly created backing field, in order to get rid of __propBackingField2735t34 like names on the client.

Also in explicitly coded properties some logic for validation can be included which is not the case for autoproperites

咽泪装欢 2024-12-10 00:18:49

这是 C# 3.0 的增强功能,称为自动实现属性,编译器在后台自动创建私有支持字段

This is a enhancement from C# 3.0 which is termed as auto implemented properties, a private backing field is automatically created by the compiler in the background

百善笑为先 2024-12-10 00:18:49

本身没有功能差异。但是如果您想要在属性设置/获取上获得更多功能,您可以使用带有私有变量的版本。

    public int EmpCode
    {
        get { return _strEmpCode > 0 ? 100 + _strEmpCode : 0; }
        set
        {
            if (value > 0)
                _strEmpCode = value;
        }
    }

否则您可以简单地使用不带有私有变量的版本。

there is no functional difference as such.. but if you want more functionality on your property setting/getting you may use the version with the private variables..

    public int EmpCode
    {
        get { return _strEmpCode > 0 ? 100 + _strEmpCode : 0; }
        set
        {
            if (value > 0)
                _strEmpCode = value;
        }
    }

Otherwise you can simply use the version without the private variables.

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