Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 551781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:25:50+00:00 2026-05-13T11:25:50+00:00

This property in a type with no access modifier (thus internal access): class SomeType

  • 0

This property in a type with no access modifier (thus internal access):

class SomeType {
    private int length;
    internal int Length {
        get { return length; }
        set length = value; }
    }
}

allows all types within the assembly of SomeType to use get and set accessors. Problem: how to restrict the access to set to only types derived from SomeType (and SomeType indeed)?

internal int Length {
    get { return length; }
    protected set length = value; }
}

is rejected by the compiler, because protected is said to be less restrictive than internal (supposedly: protected has an intersection with internal, but is not entirely included in internal –> Derived types can exist beyond the scope of internal).

What would be the code to have get accessed by any type within the assembly, and set only by derivated types within the assembly?

Edit: after reviewing the answers, I think I need to add another characteristic of the property, since it may make a difference in the solution: the type of the property is actually SomeType. The edited code is:

class SomeType {
    private SomeType length;
    internal SomeType Length {
        get { return length; }
        set length = value; }
    }
}

If the property is declared public, then the compiler issues an error (the property type SomeType is less accessible the property Length).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:25:50+00:00Added an answer on May 13, 2026 at 11:25 am

    (EDIT: I’ve just checked, and this works even when the type of the property is the same as the declaring type. However, it doesn’t work when you’re trying to declare a property within a public type where the type of the property is an internal type.)

    You can’t quite do that in C# (strictly speaking), but you can do something very similar:

    protected internal int Length { get; protected set; }
    

    (This is using an automatically implemented property just for simplicity; the same technique would work for a “normal” property too.)

    This will make the “getter” accessible to any type within the same assembly and derived types; the “setter” will only be accessible to derived types. As your class is internal anyway, this is pretty much equivalent anyway – the getter would theoretically be accessible to types outside the assembly, but as the class is internal, nothing from a different assembly should be deriving from your type anyway.

    The problem is that properties require that one access level is a “subset” of the other; internal and protected don’t work like that – one type can be in the same assembly but not derived from the type in question; another type can be derived from it but in a different assembly. They’re orthogonal, basically.

    The above solution works because protected internal means it’s accessible to any type which is either in the same assembly or derived from the type. Clearly each of protected and internal individually is a subset of this.

    You would be able to make an internal property which was further restricted for the setter if C# had some equivalent to the CLR “family and assembly” access level. (protected internal is equivalent to “family or assembly”.) Unfortunately for you, it doesn’t 🙁

    If you really want the originally stated goals (e.g. if you later have a public class you want to apply the same restrictions to), you’ll have to make at least one of them a separate method instead, e.g.

    private int length;
    internal int Length { get { return length; } }
    
    protected void SetLength(int value)
    {
        this.length = value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have a MY_Controller class with this property: class MY_Controller extends CI_Controller { public
I'm trying to understand why this ajax called doesn't work $.ajax({ type: 'GET', url:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty states: configurable : True if and only if the type of this property
I have this retained property declared like this: @property (nonatomic, retain) NSMutableDictionary *codes; then
Object doesn't support this property or method It's this line. pthumb = $(#pthumb).attr(src); Does
I am trying to do something like this: property = 'name' value = Thing()
If we define a property as public property and in this property we have
I have a variable declared like this: @property (nonatomic, retain) NSMutableArray *aInfo; At the
The documentation for the toolbar property in UINavigationController says: This property contains a reference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.