Can access modifiers be allocated for get and set accessors properties?

Can we specify access modifiers for the get and set accessors properties in C # /. NET?

If so, what would be the best approach to implement this?

+5
source share
3 answers

Yes it is possible. It is called asymmetric accessibility, and you can read the MSDN documentation for it on this page . The code will look something like this:

public int Age
{
    get
    {
        return _age;
    }
    protected set
    {
        _age = value;
    }
}

However, there are a few important caveats:

  • Only one accessor can be changed.
  • , , , , .
  • .
+11

, ...

   public class Example
{
    public string Property
    {
        get;
        private set;
    }

    public string Property2
    {
        get;
        protected set;
    }
}

..

+3

http://msdn.microsoft.com/en-us/library/ms173121.aspx shows the possible modifiers. If you want to have different modifiers, write:

[Modifier] [DataType] ProperyName{
    [Modifier] get{}
    [Modifier] set{}
}

However, if you declare internal modifiers, they must be less or equally visible than external ones.

+1
source

All Articles