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

  • Home
  • SEARCH
  • 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 437801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:36:53+00:00 2026-05-12T20:36:53+00:00

Is it possible to specify that members of a nested class can be accessed

  • 0

Is it possible to specify that members of a nested class can be accessed by the enclosing class, but not other classes ?

Here’s an illustration of the problem (of course my actual code is a bit more complex…) :

public class Journal
{
    public class JournalEntry
    {
        public JournalEntry(object value)
        {
            this.Timestamp = DateTime.Now;
            this.Value = value;
        }

        public DateTime Timestamp { get; private set; }
        public object Value { get; private set; }
    }

    // ...
}

I would like to prevent client code from creating instances of JournalEntry, but Journal must be able to create them. If I make the constructor public, anyone can create instances… but if I make it private, Journal won’t be able to !

Note that the JournalEntry class must be public, because I want to be able to expose existing entries to client code.

Any suggestion would be appreciated !


UPDATE: Thanks everyone for your input, I eventually went for the public IJournalEntry interface, implemented by a private JournalEntry class (despite the last requirement in my question…)

  • 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-12T20:36:53+00:00Added an answer on May 12, 2026 at 8:36 pm

    If your class is not too complex, you could either use an interface which is publicly visible and make the actual implementing class private, or you could make a protected constructor for the JornalEntry class and have a private class JornalEntryInstance derived from JornalEntry with a public constructor which is actually instantiated by your Journal.

    public class Journal
    {
        public class JournalEntry
        {
            protected JournalEntry(object value)
            {
                this.Timestamp = DateTime.Now;
                this.Value = value;
            }
    
            public DateTime Timestamp { get; private set; }
            public object Value { get; private set; }
        }
    
        private class JournalEntryInstance: JournalEntry
        { 
            public JournalEntryInstance(object value): base(value)
            { }
        }
        JournalEntry CreateEntry(object value)
        {
            return new JournalEntryInstance(value);
        }
    }
    

    If your actual class is too complex to do either of that and you can get away with the constructor being not completely invisible, you can make the constructor internal so it is only visible in the assembly.

    If that too is infeasible, you can always make the constructor private and use reflection to call it from your journal class:

    typeof(object).GetConstructor(new Type[] { }).Invoke(new Object[] { value });
    

    Now that I think about it, another possibility would use a private delegate in the containing class which is set from the inner class

    public class Journal
    {
        private static Func<object, JournalEntry> EntryFactory;
        public class JournalEntry
        {
            internal static void Initialize()
            {
                Journal.EntryFactory = CreateEntry;
            }
            private static JournalEntry CreateEntry(object value)
            {
                return new JournalEntry(value);
            }
            private JournalEntry(object value)
            {
                this.Timestamp = DateTime.Now;
                this.Value = value;
            }
    
            public DateTime Timestamp { get; private set; }
            public object Value { get; private set; }
        }
    
        static Journal()
        {
            JournalEntry.Initialize();
        }
            
        static JournalEntry CreateEntry(object value)
        {
            return EntryFactory(value);
        }
    }
    

    This should give you your desired visibility levels without needing to resort on slow reflection or introducing additional classes / interfaces

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If using Fluent NHibernate, is it possible to automap most classes, but specify that
How to define a struct whose members can be accessed by any internal classes
is it possible to specify that a NSMutableArray can only contain a certain type
Is it possible that when I use the getText(keyName); in action class, I can
From a webpage, I can specify that a SWF should be loaded with allowNetworking=internal
Is it possible to specify that a @string value belongs to a different package
Is it possible to specify that as SVG image should produce output in CMYK?
Hi is it possible to specify a method that is the default method for
I have seen that you can specify what to do generally if an ajax
I have that template class that uses a policy for it's output and another

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.