Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

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

I have the 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 for Oracle};Server=CTIR; UID="+dbUsername+";PWD="+dbPassword+";");    
        }        
    }
}

Now I get the following error:

Error 11: The accessibility modifier of the 'Framework.OracleProvider.db.set' accessor must be more restrictive than the property or indexer 'Framework.OracleProvider.db'

I've been looking at similar questions but haven't really found an answer.

Can anyone explain to me why this is happening? I really want to learn.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the problem:

private OdbcConnection db { get; private set; }

Assuming you really want both the getter and setter to be private, this should be:

private OdbcConnection db { get; set; }

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

Alternatively, if you want the getter to be non-private and the setter to be private, you need to specify some other modifier, e.g.

internal OdbcConnection db { get; set; }

Basically, if you're going to specify an access modifier on the get; or set; part of a property, it has to be more restrictive than it would otherwise be.

From section 10.7.2 of the C# specification:

The accessor-modifier must declare an accessibility that is strictly more restrictive than the declared accessibility of the property or indexer itself. To be precise:

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

(As an aside, if it's private for both reading and writing, it would probably be better just to use a field. Most of the benefits of using a property are only present if it's exposed beyond the current class. And if you do keep it as a property, consider renaming it to follow normal .NET naming conventions.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...