c# - accessor must be more restrictive than the property or indexer -


i have folowing class:

using system; using system.collections.generic; using system.linq; using system.text; using system.data.odbc;  namespace framework {     public class oracleprovider     {         private odbcconnection db { get; private set; }         private string dbusername = settings.default.username;         private string dbpassword = settings.default.password;          public oracleprovider()         {             connect();         }          public void connect()         {              db = new odbcconnection("driver={microsoft odbc oracle};server=ctir; uid="+dbusername+";pwd="+dbpassword+";");             }             } } 

now following error:

error 11: accessibility modifier of 'framework.oracleprovider.db.set' accessor must more restrictive property or indexer 'framework.oracleprovider.db'

i've been looking @ similar questions haven't found answer.

can explain me why happening? want learn.

this problem:

private odbcconnection db { get; private set; } 

assuming want both getter , setter private, should be:

private odbcconnection db { get; set; } 

the setter already private, that's accessibility of overall property.

alternatively, if want getter non-private , setter private, need specify other modifier, e.g.

internal odbcconnection db { get; set; } 

basically, if you're going specify access modifier on get; or set; part of property, has more restrictive otherwise be.

from section 10.7.2 of c# specification:

the accessor-modifier must declare accessibility strictly more restrictive declared accessibility of property or indexer itself. precise:

  • if property or indexer has declared accessibility of public, accessor-modifier may either protected internal, internal, protected, or private.
  • if property or indexer has declared accessibility of protected internal, accessor-modifier may either internal, protected, or private.
  • if property or indexer has declared accessibility of internal or protected, accessor-modifier must private.
  • if property or indexer has declared accessibility of private, no accessor-modifier may used.

(as aside, if it's private both reading , writing, better use field. of benefits of using property present if it's exposed beyond current class. , if do keep property, consider renaming follow normal .net naming conventions.)


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -