C#如何理解属性限制?代码详细指南

2021年3月23日14:20:38 发表评论 688 次浏览

先决条件:C#中的属性

属性是类成员的特殊类型, 它提供了一种灵活的机制来读取, 写入或计算私有字段的值。可以将属性当作公共数据成员使用, 但实际上它们是称为访问器的特殊方法。这样可以轻松访问数据, 并有助于提高方法的灵活性和安全性。信息的封装和隐藏也可以使用属性来实现。它使用预定义的方法, 这些方法是" get"和" set"方法, 有助于访问和修改属性。

语法如下:

<access_modifier> <return_type> <property_name>
{
        get { // body }
        set { // body }
}

属性限制:在编写如下属性时, 我们必须遵循一些规则或限制:

属性不能通过ref或out参数传递给方法:属性不能通过out或ref传递,因为属性实际上是方法。在编译时,ref和out都不被认为是方法签名的一部分。因此,它将给出一个编译时错误。除了从程序中完全删除ref或out之外,没有办法解决这个限制。

不能重载属性:不能重载属性。这意味着只能分别在一个类中放置一个get和set访问器和mutator。下面给出的程序展示了在同一个类中提供多个get访问器时会发生什么。

// C# program to show what happens when
// we try to overload a property
using System;
  
class Myprop {
  
     int _number;
  
     public int Number
     {
         get
         {
             return this ._number;
         }
         set
         {
             this ._number = value;
         }
         get
         {
  
             // Causes a compile time error
             // which tells get accessor
             return this ._number; 
  
         } // has already been called.
     }
}
  
// Driver Class
class GFG {
  
     // Main Method
     static void Main()
     {
         Myprop ex = new Myprop();
  
          // set { }
         ex.Number = 5;
         console.WriteLine( "If there are only two properties" +
                       " in class ex we will get the output" );
  
         // get { }
         Console.WriteLine(ex.Number); 
     }
}

编译时错误:

prog.cs(19, 3):错误CS1007:属性访问器已定义

但是通过使用不同类别的属性在同一个程序中, 可以多次使用get和set方法。下面给出了说明这一点的示例。

// C# program to demonstrate the use
// of properties in different classes
using System;
  
class Myprop {
  
     int _number;
  
     public int Number
     {
         get
         {
             return this ._number;
         }
         set
         {
             this ._number = value;
         }
     }
}
  
  
class Myprops {
  
     int _number;
     public int Number
     {
         get
         {
             return this ._number;
         }
         set
         {
             this ._number = value;
         }
     }
}
  
// Driver Class
class GFG {
  
     // Main Method
     static void Main()
     {
         Myprop ex = new Myprop();
         Myprops exs = new Myprops();
  
         // Calls set mutator from the Myprops class
         exs.Number = 7; 
  
         // Calls set mutator from the Myprop class
         ex.Number = 5; 
  
         // Gets the get accessor form the Myprop class
         Console.WriteLine( "The value set in the class " +
                              "Myprop is: " + ex.Number); 
  
         // Gets the get accessor form the Myprops class
         Console.WriteLine( "The value set in the class" +
                          " Myprops is: " + exs.Number); 
     }
}

输出如下:

The value set in the class Myprop is: 5
The value set in the class Myprops is: 7

当调用get访问器时,属性不应该改变基础变量的状态:属性的get访问器被设置为只读模式,而set命令被设置为只写模式。因此,如果我们试图在get访问器的范围内输入值或修改值,就会得到一个警告,告诉我们试图访问的代码不可访问。下面给出的例子说明了这一点。

// C# program to demonstrate the 
// above-mentioned restriction
using System;
  
class Myprop {
  
     int _number;
     public int Number
     {
         get
         {
             return this ._number;
  
             // We get the warning saying that
             // the code is unreachable.
             this ._number = 5; 
         }
         set
         {
             this ._number = value;
         }
     }
}
  
// Driver Class
class GFG {
  
     static void Main()
     {
         Myprop ex = new Myprop();
  
         // set { }
         ex.Number = 5; 
  
          // get { }
         Console.WriteLine(ex.Number);
     }
}

警告:

prog.cs(16, 4):警告CS0162:检测到无法访问的代码

输出如下:

5

从程序的输出可以看出, 代码已成功运行, 但由于无法访问而无法使用我们在get访问器中添加的值。这就是为什么在调用get访问器时无需更改基础变量的原因。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: