How to make a property protected AND internal in C#?
It's not possible in C#.
Just for the sake of completeness, this is supported in IL (family and assembly access modifier).
I would keep the access modifier as protected and have an internal helper method.
protected override string[] Headers {
get { return headers; } // Note that get is protected
set { headers = value; }
}
internal SetHeadersInternal(string[] newHeaders)
{
headers = newHeaders;
}
But somehow, this smells like it should be refactored somehow. Internal is always something I'd use sparingly because it can lead to a very messy architecture where everything is somehow using everything else within the assembly, but of course there's always exceptions.
What's wrong with making the getter public? If you declare the property as
public string[] Headers { get; protected set; }
it meets all of the criteria you want: all members of the assembly can get the property, and only derived classes can set it. Sure, classes outside the assembly can get the property too. So?
If you genuinely need to expose the property within your assembly but not publicly, another way to do it is to create a different property:
protected string[] Headers { get; set; }
internal string[] I_Headers { get { return Headers; } }
Sure, it's ugly decorating the name with that I_
prefix. But it's kind of a weird design. Doing some kind of name mangling on the internal property is a way of reminding yourself (or other developers) that the property they're using is unorthodox. Also, if you later decide that mixing accessibility like this is not really the right solution to your problem, you'll know which properties to fix.