6

I have a class that is internal by itself, so the following difference in declaration has no real-world consequence but I would like to understand the mechanics behind it.

I have a property like this:

public String CreditedAs { get; protected set; }

and it compiles just fine. The getter can be read by anyone that has access to the class, the setter only within the class or classes derived by it.

Now when I try this:

internal String CreditedAs { get; protected set; }

I get

The accessibility modifier of the '{class}.CreditedAs.set' accessor
must be more restrictive than the property or indexer '{class}.CreditedAs'

Why is that? And

protected String CreditedAs { internal get; set; }

doesn't work either.

Dee J. Doena
  • 1,631
  • 3
  • 16
  • 26
  • dont forget to mark it as accepted if it works for you.... – Pranay Rana Mar 05 '15 at 07:15
  • 1
    This question is not an exact duplicate of the other. Though the same error is encountered, different problems caused it. I landed here on this page and I would have never landed on the page marked as this duplicate's original. – Michael Plautz Dec 18 '17 at 18:38
  • I voted to reopen, as the other question does not address the issue presented here. This question isn't about accessor restrictions in general, which the other question is. This question is specifically about the confusion involving accessibility modifiers that are more restrictive on one metric (e.g. types) but less restrictive on another (e.g. assembly). This question adds value and should stand on its own. – Max Vollmer Apr 19 '23 at 13:07

1 Answers1

5

I think you are confuse between protected and internal

internal - says that type is accessible with in the assembly only. not outside assembly.

protected - says that type is accessible in the given type and in the type which derived from the base type.

So if you use like as you explain create problem.

So if you want to achieve both functionality you can use Protected internal - which says that type is available to in type and in derived type , and available with in the assembly only.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • In addition to this correct explanation: note that it is possible to derive from a non sealed class in assembly A in another assembly B. In that case class B has access to protected members of class A, but not to internal members. – Henk Kok Jul 12 '18 at 16:07