Is it possible to have different access modifiers on the get/set methods of a property? Justify

Showing Answers 1 - 48 of 48 Answers

ranjik2008

  • Dec 14th, 2007
 

Yes,It is possible to have different access modifiers,but the best approach is to use all access modifiers as public for efficient data retrieval.
public String EMPNO

{

get

{

return _empno;

}

set

{

_empno =
value;

}

}

  Was this answer useful?  Yes

ranjik2008

  • Dec 14th, 2007
 

Yes,It is possible to assign different access modifiers,but the efficient approach is make all modifiers to public for accessibility
public
String EMPNO

{

get

{

return _empno;

}

set

{

_empno =
value;

}

}

  Was this answer useful?  Yes

factpandit

  • Dec 16th, 2007
 

I think the right answer is No, if I understand the question correctly.

We don't specify any access modifiers on "get" and "set" individually, rather we specify on the wrapping property name.

in other words following is valid:

private int m_myInt = -1;
public int MyProperty
{
get {return m_myInt;}


set {m_myInt = value;}
}


However following is not valid

private int m_myInt = -1;
public int MyProperty
{
public get{return m_myInt;}


private set{m_myInt = value;}
}



You can have different access modifiers applied to the get and set methods.

public class Myclass
{
    private string _MyProperty;
    public string MyProperty
    {
       get { return _MyProperty; }
       internal set { _MyProperty= value; }
    }
}

Thus types in the same assembly as MyClass have access to the  set method.However the get method is public.This situation is very common in day to day applications where only a specified set of classes can set the value while most of the classes can read the values.

It is very easy, if you are using .NET Framework 2.0 or above. See http://msdn.microsoft.com/en-us/library/75e8y5dd(VS.80).aspx

In short, the restricion is always to a more restrictive level, and only one accessor can be modified - for instance:
public int MyInt
{
   get
   { return m_MyInt; }
   private set
   { m_MyInt = value; }
}

will compile, but:

private int MyInt
{
   get
   { return m_MyInt; }
   public set
   { m_MyInt = value; }
}
will not compile, nor will:

public int MyInt
{
   protected get
   { return m_MyInt; }
   private set
   { m_MyInt = value; }
}

amasood

  • Aug 18th, 2008
 

Yes : for set method i.e protected, and

No:for get method

e.g
public class A
{
   private string name;
  
   public string Name
  {
      get
     {
          return name;
      }
      protected set
      {
             name=value;
       }
   }
}

  Was this answer useful?  Yes

Yes you can do it in .net 2.0 onwards

string name;
public string Name
{
get { return name; }
protected set
{

name = value;
}
}

You cannot specify access modifier for both getter and setter at same time. The other will always take the default from property.

PeterVH

  • Aug 19th, 2011
 

Yes you can, but your setter can't be more accessable than your getter (logical, as by setting you ask the instance first for the reference to the place in memory where you want to write, i.e. get)

Important example : stability of the class, cfr attached code. If you could have a potential nullreferenceException on a property, it is the responsability of the class and only that class to avoid that possibility (private setter)

Code
  1.     public partial class Form4 : Form

  2.     {

  3.         public Form4() { InitializeComponent(); }

  4.  

  5.         private void button1_Click(object sender, EventArgs e)

  6.         {

  7.             // DANGER, public setter. Okay, stupid example to put it to null explicitly

  8.             // but in theory the List of string is a returned list from another function

  9.             // so might be null..

  10.             var example = new Example { BadDesign = null };

  11.  

  12.             // impossible because of private set, won't compile

  13.             // example = new Example { GoodDesign = null };

  14.  

  15.             foreach (var word in new List<string> { "This", "will", "work?" })

  16.                 example.GoodDesign.Add(word);

  17.  

  18.             foreach (var word in new List<string> { "Will", "get", "nullref","exception!" })

  19.                 example.BadDesign.Add(word);

  20.         }

  21.     }

  22.  

  23.     public class Example

  24.     {

  25.         public List<string> BadDesign { get; set; }

  26.         public List<string> GoodDesign { get; private set; }

  27.  

  28.         public Example()

  29.         {

  30.             BadDesign = new List<string>();

  31.             GoodDesign = new List<string>();

  32.         }

  33.     }

  Was this answer useful?  Yes

When we create a property internally CIL creates two methods.

.method public hidebysig specialname instance int32 get_MyProperty()
.method public hidebysig specialname instance void set_MyProperty(int32 'value')

The default access specifier for these method is equal to the access specifer of
the property.
So we can change the access specifier of the getter and setter method at the compile time.
what we cannot do is

1) We cannot change access specifier of getter and setter method at the same time.

public int MyProperty
{
protected get { return 20; }
protected set { value = 10; }
}

2) We cannot have access specifier of getter and setter method greater than that of accesss specifier of Property.

private int MyProperty
{
get { return 20; }
internal set { value = 10; }
}

What we can do is

3)

public int MyProperty
{
private get { return 20; }
set { value = 10; }
}

  Was this answer useful?  Yes

Yes you can. However you can set the access modifier for either get or set.

For ex:

private int i;

public int I
{
protected get { return i; }
set { i = value; }
}


OR

private int i;

public int I
{
get { return i; }
protected set { i = value; }
}

Setting the access modifier for both of them would result in the following compilation error.

Error 1 Cannot specify accessibility modifiers for both accessors of the property or indexer

Also , you can't set them as public. This would result in the following compilation error.

Error 1 The accessibility modifier of the '******.get' accessor must be more restrictive than the property or indexer

  Was this answer useful?  Yes

LordAlex

  • Nov 3rd, 2011
 

The accessor modifier must declare an accessibility that is more restrictive than the declared accessibility of the property itself.

  Was this answer useful?  Yes

S Manasa

  • Jul 8th, 2019
 

Generally, the access modifier of the property is considered as the access modifier of both the accessors if no access modifier is specified for the accessors.
Only one of the accessor can be specified with an access modifier and that should be more restrictive than the Property access modifier

Code
  1. public class TestClass

  2. {

  3.     private string name = "Abc";

  4.     private string id = "1234;

  5.  

  6.    public string Name

  7.    {

  8.        get

  9.        {

  10.            return name;

  11.        }

  12.       private set

  13.        {

  14.            name = value;

  15.        }

  16.    }

  17.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions