Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Property with internal set and protected get

I want to do so a variable (let's call it Foo) that can only be modified within the assembly but can be acessed by any child class, even outside the assembly So I want to have an internal set and a protected get:

Doing protected int Foo { internal set; get; } tells me this that the set must be more restrictive than the property or indexer But internal int Foo { set; protected get; } tells me the same thing for the get

How can I solve that?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can create an internal method (or proxy property) to call the array.

 protected int Foo { internal set; get; } internal void SetFoo(int foo) { Foo = foo; }

In this case, you can set the setter of Foo to private .

Note that this allows anything in your assembly that has a reference to this object to be able to call SetFoo , which may not be what you want.

over 4 years ago · Santiago Trujillo Report

0

Use internal for the setter and protected internal for the whole property.

public class X
{
    // Read from this assembly OR child classes
    // Write from this assembly
    protected internal int Foo { internal set; get; }

}

The name protected internal is a bit misleading, but does what you want to achieve:

A protected internal member is accessible from the current assembly or from types that are derived from the containing class.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected-internal

So, because the setter is internal, you can set the property from anywhere in the same assembly but not from a different assembly. And because the property as a whole is protected internal, you can read it from anywhere in the same assembly OR from child classes in any assembly.


Another approach:

public class X
{
    // Read from child classes 
    // Write only from child classes in the same assembly
    protected int Foo { private protected set;  get; }

}

A private protected member is accessible by types derived from the containing class, but only within its containing assembly.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/private-protected

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!