获取和设置同一属性的访问器可能具有不同的访问修饰符。 [英] get and set accessors for the same property may have different access modifiers.

查看:73
本文介绍了获取和设置同一属性的访问器可能具有不同的访问修饰符。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我认为访问修饰符适用于该属性。您能否从MSDN文档中澄清以下行。你能不能给我一个相同的例子

I thought the access modifier are applicable to the property. Can you please clarify the below line from the MSDN documentation. Can you kindly give me an example of the same

获取
设置 同一属性的访问器可能具有不同的访问修饰符。例如,
get 可能是
public 允许从类型外部进行只读访问,以及
set 可能是
private
protected

The get and set accessors for the same property may have different access modifiers. For example, the get may be public to allow read-only access from outside the type, and the set may be private or protected.

问候

Manoj Gokhale

Manoj Gokhale




推荐答案

   public class MyClass
   {
      // This property has a public "get" and a private "set".
      public string stringProperty { get; private set; }

      public MyClass(string prop)
      {
         stringProperty = prop;
      }
   }


上面的类可以像这样使用。

The above class could be used like so.

         MyClass mc = new MyClass("test string");

         // This works, because the get is public.
         textBox1.Text = mc.stringProperty;

         // This produces an error, because the set is private.
         mc.stringProperty = "another test";


这篇关于获取和设置同一属性的访问器可能具有不同的访问修饰符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆