.net - 在 .NET 中将成员对象公开为属性或方法

标签 .net class properties methodology

在 .NET 中,如果一个类包含一个作为类对象的成员,该成员应该作为属性公开还是使用方法公开?

最佳答案

您应该为任何概念上表示对象状态的东西使用属性,只要它的检索不是一个足够昂贵的操作,您应该避免重复使用它。

来自 MSDN :

Class library designers often must decide between implementing a class member as a property or a method. In general, methods represent actions and properties represent data. Use the following guidelines to help you choose between these options.

  • Use a property when the member is a logical data member. In the following member declarations, Name is a property because it is a logical member of the class.

    public string Name
    get 
    {
        return name;
    }
    set 
    {
        name = value;
    }
    
  • Use a method when:

    • The operation is a conversion, such as Object.ToString.
    • The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
    • Obtaining a property value using the get accessor would have an observable side effect.
    • Calling the member twice in succession produces different results.
    • The order of execution is important. Note that a type's properties should be able to be set and retrieved in any order.
    • The member is static but returns a value that can be changed.
    • The member returns an array. Properties that return arrays can be very misleading. Usually it is necessary to return a copy of the internal array so that the user cannot change internal state. This, coupled with the fact that a user can easily assume it is an indexed property, leads to inefficient code. In the following code example, each call to the Methods property creates a copy of the array. As a result, 2n+1 copies of the array will be created in the following loop.

      Type type = // Get a type.
      for (int i = 0; i < type.Methods.Length; i++)
      {
         if (type.Methods[i].Name.Equals ("text"))
         {
            // Perform some operation.
         }
      }
      

The following example illustrates the correct use of properties and methods.

    class Connection
    {
       // The following three members should be properties
       // because they can be set in any order.
       string DNSName {get{};set{};}
       string UserName {get{};set{};}
       string Password {get{};set{};}

       // The following member should be a method
       // because the order of execution is important.
       // This method cannot be executed until after the 
       // properties have been set.
       bool Execute ();
    }

关于.net - 在 .NET 中将成员对象公开为属性或方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/164527/

相关文章:

c++ - 在 header 中声明对象

swift - 创建 Swift 属性

kotlin - 如何获取在 kotlin 代码中的 'gradle.properties 中定义的自定义属性?

c# - MapControllerRoute ,值不能为 null

Javascript - 将类添加到类(不是 ID)

.net - VB.net 函数中是否有任何 IN 运算符,如 SQL 中的运算符

javascript - 从 React-Native 应用程序中的另一个类访问静态变量?

javascript - 这是检查属性是否存在或分配一个的安全方法吗?

.net - 在 Silverlight 中使用绑定(bind)时连接字符串的方法

c# - 如何从 RTF 文件中获取文本大小和颜色?