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 6360589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:40:03+00:00 2026-05-24T23:40:03+00:00

I have read several articles on when to use nested classes, but none that

  • 0

I have read several articles on when to use nested classes, but none that I’ve found address my specific question.

C# has a class called XmlReader which only provides a Create() method. I’m assuming that the create creates a subclass of XmlReader. If not, then for this example, assume that it does.

Consider this relationship:

/// <summary>
/// Class to read information in a file on disk
/// </summary>
interface ILoad
{
  /// <summary> Version number of the file </summary>
  int Version {get;}

  /// <summary> Content of the file </summary>
  string Content {get;}

  /// <summary> Full path to the file </summary>
  string FullPath {get;}
}

/// <summary> Provides base loading functionality </summary>
class LoaderBase : ILoad
{
    public int Version {get; protected set;}
    public string Content {get; protected set;}
    public string FullPath{get; protected set;}

    /* Helpers omitted */
    protected abstract void Load(string pathToFile);

    public static LoaderBase Create(string pathToFile)
    {
      switch(Path.GetExtension(pathToFile))
      {
         // Select the correct loader based on the file extension and return
      }
      return null;//unknown file type
    }
}

/// <summary> Base class functionality to load compiled files </summary>
public abstract class CompiledLoaderBase : LoaderBase
{
  protected CompiledLoaderBase(string path)
  {
    Load(path);
  }

  protected override Load(string path)
  {
     /* read the file and create an XmlReader from it */
     ReadVersionNumber(reader);
     ReadContent(reader); 
  }

  protected abstract void ReadVersionNumber(XmlReader reader);
  protected abstract void ReadContent(XmlReader reader);

  // Wish I could call this Create, but inherited a static Create method already
  public static CompiledLoaderBase CreateCompiled(string path)
  {
     //Figure out which loader to create and return it
     // ... Assume we figured out we need V1
     return new CompiledLoaderV1(path);
  }


  // Here's the fun stuff!
  protected class CompiledLoaderV1 : CompiledLoaderBase
  {
    public CompiledLoaderV1(string path)
    : base(path)
    {}

    protected override ReadVersionNumber(XmlReader reader)
    { /* read the version number and store in Version */ }

    protected override ReadContent(XmlReader reader)
    { /* read the content and store in Content */ }
  }

  // ... More classes with their own methods for reading version and content

}

Now, I used nested classes to prevent the user from creating the specific loaders directly; they must use one of the abstract base’s Create* methods. FxCop blew up in my face over this and I was hoping to get some clarification on why.

It mentioned not to use nested classes, but instead namespaces. Is there a way to accomplish this with namespaces?
EDIT: Specifically, the message is: “NestedTypesShouldNotBeVisible”. Resolution: “Do not nest type ‘CompiledLoaderBase+CompiledLoaderV1’. Alternatively, change its accessibility so that it is not externally visible.” Info: “Do not use public, protected, or protected internal nested types as a way of grouping types. Use namespaces for this purpose. There are very limited scenarios where nested types are the best design.” Now, I believe Jon Skeet identified that you cannot accomplish this with namespaces. I just wanted to make sure since this error says that there are a limited scenarios where this is the best design, so if there is a better one, I’m open to ideas 😀

Also, it did not like virtual call chain called from constructor. Is there a reason for this? Is there a way around it?
EDIT: Specifically, the message is: “DoNotCallOverridableMethodsInConstructors”. Resolution: “‘CompiledLoaderV2.CompiledLoaderV2(String)’ contains a call chain that results in a call to a virtual method defined by the class. Review the following call stack for unintended consequences”
Info: “Virtual methods defined on the class should not be called from constructors. If a derived class has overridden the method, the derived class version will be called (before the derived class constructor is called)”. I feel like this could be a problem if the subclasses did something in their constructors, but since they do not, I’m not sure this is an issue. Is there a better way to force the classes to load in a certain way without using abstract methods in the constructor?

Thanks so much for all your help!

  • 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-24T23:40:04+00:00Added an answer on May 24, 2026 at 11:40 pm

    No, you can’t do this with namespaces, although you can do it with assemblies – i.e. prevent anyone outside the assembly from creating an instance.

    You can absolutely do it with nested classes, but you generally should make the constructor itself private to prevent anything else deriving from the class. You can also make the nested classes themselves private unless you need to them to the outside world.

    You can use this pattern to create something like Java enums, and also limited factories. I’ve used it for a discriminated union in Noda Time – the actual details don’t matter, but you might like to look at the source for more inspiration.

    You’re right to mistrust calling virtual methods from constructors. It can occasionally be useful but should be done very carefully with heavy documentation.

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

Sidebar

Related Questions

I'm quite confused about several books in .NET that I have read. Would someone
I have read several articles regarding templates for web site content. They all seem
I have a MEF based solution that has several exported implementations of an interface.
I have lately read several articles and questions about Model Binding and Validation in
I have read at several places that transport security is only hop to hop
I have read several documentations about building custom ErrorHandler (by inheriting from IErrorHandler). Unfortunately,
I have read through several reviews on Amazon and some books seem outdated. I
I have read a lot that LISP can redefine syntax on the fly, presumably
I have read that using database keys in a URL is a bad thing
I have read on Stack Overflow some people that have converting to C#2.0 to

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.