Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# internal getter, protected setter with an internal class parameter

I was having the problem of wanting a property to have an internal getter and a protected setter, as described in this question, and I thought I solved that by doing the following:

public class Accessor : AccessorBase
{
    private Connection _connection;

    protected void setConnection(Connection value)
    {
        _connection = value;
    }

    internal Connection GetConnection()
    {
        return _connection;
    }
    ...
}

However, I'm now getting this error:

Inconsistent accessibility: parameter type 'Connection' is less accessible than method 'setConnection(Connection)'

This is because I have internal class Connection. I would rather not make Connection a public class, while Accessor needs to be public, so how can I get around this error while still maintaining an internal getter and a protected setter?

like image 417
Sarah Vessels Avatar asked Jan 07 '10 19:01

Sarah Vessels


1 Answers

Unfortunately C# doesn't support "internal and protected" access modifiers (only "internal or protected" is supported), which means any protected members are visible outside the assembly and can't use an internal type.
Using internal instead of protected would be the most logical solution.

And you could vote at Microsoft Connect so that it might be added to C# someday.

Update: as of C# 7.2 you can use private protected for this.

like image 149
Pent Ploompuu Avatar answered Sep 17 '22 20:09

Pent Ploompuu