首页>Program>source

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

最新回答
  • 11天前
    1 #

    如果您要做的只是公开对象实例, 与当前对象的状态有关,您应该使用属性。

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

  • 11天前
    2 #

    对于概念上代表对象状态的任何事物,都应使用属性,只要对象的检索操作不够昂贵,就应避免重复使用它。

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

      在以下情况下使用方法:

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

        以下示例说明了属性和方法的正确使用。

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

    那无关紧要。

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

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

  • 11天前
    3 #

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

    仅需澄清一下,通常应假定属性代表对象的内部状态.但是,将成员实现为属性或方法告诉用户调用可能会花费多少。

  • 11天前
    4 #

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

    方法对分配给该类的数据进行处理。

  • php:Magento产品分类
  • javascript:引导日期选择器从另一个日期选择器更改minDate / startDate