汽车房产有什么意义?

时间:2011-02-22 03:41:28

标签: c# automatic-properties

这可能听起来很幼稚,但是......

class Widget
{
    public int Foo { get; set; }
}

这很酷,并且使用支持字段来节省一些样板,但在那时,它不等于简单:

class Widget
{
    public int Foo;
}

似乎它只不过是一个公共领域,尽管我认为它看起来与众不同。但是,从设计的角度来看,如果不利用封装,使用属性有什么好处?

6 个答案:

答案 0 :(得分:24)

因为它为您提供了以后添加封装逻辑的潜力,而无需更改类的元数据。

使用属性被认为是最佳实践 - 自动实现的属性旨在消除编写属性的繁琐,以鼓励开发人员遵循此最佳实践

答案 1 :(得分:10)

除了到目前为止发布的其他好答案:

  • 很容易快速创建一个private-setter-public-getter属性,这可以说是一个比公共只读字段更好的做法,用于创建不可变数据类型。

答案 2 :(得分:2)

  1. 属性允许您向类中添加封装
  2. 属性允许您的访问是多态的(如果您选择,继承者可以在属性为虚拟时修改访问权限)。
  3. 当您处理简单的get / set操作时,自动属性很好。如果你在get / set中执行更复杂的操作,那么就不能使用auto-property。
  4. 同样,反射在属性上的作用与在变量上的作用不同,但是,您使用的是MemberInfo(FieldInfo,PropertyInfo或MethodInfo,无论您选择哪种方式)。

答案 3 :(得分:0)

  1. 任何程序员可能因任何原因需要遵循的某种标准
  2. 反射在属性上的工作方式与在变量上的工作方式不同
  3. 您无法对变量进行数据绑定
  4. 如果您决定将其更改为属性
  5. ,则必须重建使用该变量的所有代码

答案 4 :(得分:0)

来自msdn

  

属性结合了字段和方法的各个方面。对于对象的用户,属性似乎是一个字段,访问该属性需要相同的语法。

您可以这样做:

public class Date
{
private int month = 7;  // Backing store

public int Month
{
    get
    {
        return month;
    }
    set
    {
        if ((value > 0) && (value < 13))
        {
            month = value;
        }
    }
}
}

简单地说,属性更加通用。

答案 5 :(得分:-1)

当我们实现自动属性时,这就是“幕后”发生的事情:

你的班级:

public class YourClass {
    public int Foo { get; set; }
}

IL 代码:

.class private auto ansi '<Module>'
{
} // end of class <Module>

.class public auto ansi beforefieldinit YourClass
    extends [System.Private.CoreLib]System.Object
{
    // Fields
    .field private int32 '<Foo>k__BackingField'  //this is the backing field for your property
    .custom instance void [System.Private.CoreLib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
        01 00 00 00
    )

    // Methods
    .method public hidebysig specialname 
        instance int32 get_Foo () cil managed //this is the GETTER of your property
    {
        .custom instance void [System.Private.CoreLib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x2050
        // Code size 7 (0x7)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldfld int32 YourClass::'<Foo>k__BackingField'
        IL_0006: ret
    } // end of method YourClass::get_Foo

    .method public hidebysig specialname 
        instance void set_Foo (
            int32 'value'
        ) cil managed //this is the SETTER of your property
    {
        .custom instance void [System.Private.CoreLib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x2058
        // Code size 8 (0x8)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldarg.1
        IL_0002: stfld int32 YourClass::'<Foo>k__BackingField'
        IL_0007: ret
    } // end of method YourClass::set_Foo

    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed 
    {
        // Method begins at RVA 0x2061
        // Code size 7 (0x7)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void [System.Private.CoreLib]System.Object::.ctor()
        IL_0006: ret
    } // end of method YourClass::.ctor

    // Properties
    .property instance int32 Foo()
    {
        .get instance int32 YourClass::get_Foo()
        .set instance void YourClass::set_Foo(int32)
    }

} // end of class YourClass


  • 如果我们的 getter 和/或 setter 没有任何自定义逻辑,我们将使用自动属性。

  • 自动属性还为我们“自动”定义了“字段”。


You can check it here too.

相关问题