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

时间:2020-03-06 15:02:59  来源:igfitidea点击:

在.NET中,如果类包含作为类对象的成员,则该成员应作为属性或者方法公开?

解决方案

财产。属性基本上只是一种"便宜"的方法。获取或者设置对对象的引用非常便宜。

为了澄清起见,通常应假定属性代表对象的内部状态。但是,将成员实现为属性或者方法会告诉用户该呼叫可能会花费多少。

属性读取并将值分配给类中的实例。

方法对分配给该类的数据执行某些操作。

这无关紧要。

如果该值是有关对象状态的某些详细信息,则应为"属性"。

如果对对象执行某些操作,则应为Method。

如果我们要做的只是公开与当前对象状态相关的对象实例,则应使用一个属性。

当我们有某种逻辑要做的事情比访问内存中的对象并返回该值还要多,或者我们执行的操作对当前对象的状态产生广泛影响时,应使用一种方法。

我们应该对任何在概念上表示对象状态的东西都使用属性,只要它的检索操作不够昂贵,我们就应该避免重复使用它。

从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 ();
    }

概述

通常,属性存储对象的数据,例如Name,而方法是可以要求对象执行的动作,例如Move或者Show。有时,不清楚哪些类成员应该是属性,哪些应该是方法是集合类(VB)的Item方法存储和检索数据,并且可以将其实现为索引属性。另一方面,将Item作为方法来实现也是合理的。

句法

类成员将如何使用也可能是决定将其表示为属性还是方法的决定因素。从参数化属性中检索信息的语法与用于实现为函数的方法的语法几乎相同。但是,修改此类值的语法略有不同。

如果将类的成员实现为属性,则必须通过以下方式修改其值:

ThisObject.ThisProperty(Index)= NewValue

如果将类成员实现为方法,则必须使用以下方法修改要修改的值:

ThisObject.ThisProperty(Index,NewValue)

失误

尝试将值分配给只读属性将返回与对方法的类似调用不同的错误消息。正确实现的类成员将返回易于解释的错误消息。