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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:16:57+00:00 2026-05-15T07:16:57+00:00

I am confused as to why the new operator isn’t working as I expected

  • 0

I am confused as to why the new operator isn’t working as I expected it to.

Note: All classes below are defined in the same namespace, and in the same file.

This class allows you to prefix any content written to the console with some provided text.

public class ConsoleWriter
{
    private string prefix;

    public ConsoleWriter(string prefix)
    {
        this.prefix = prefix;
    }

    public void Write(string text)
    {
        Console.WriteLine(String.Concat(prefix,text));
    }
}

Here is a base class:

public class BaseClass
{
    protected static ConsoleWriter consoleWriter = new ConsoleWriter("");

    public static void Write(string text)
    {
        consoleWriter.Write(text);
    }
}

Here is an implemented class:

public class NewClass : BaseClass
{
    protected new static ConsoleWriter consoleWriter = new ConsoleWriter("> ");
}

Now here’s the code to execute this:

class Program
{
    static void Main(string[] args)
    {
        BaseClass.Write("Hello World!");
        NewClass.Write("Hello World!");

        Console.Read();
    }
}

So I would expect the output to be

Hello World!
> Hello World!

But the output is

Hello World!
Hello World!

I do not understand why this is happening. Here is my thought process as to what is happening:

  1. The CLR calls the BaseClass.Write() method
  2. The CLR initialises the BaseClass.consoleWriter member.
  3. The method is called and executed with the BaseClass.consoleWriter variable

Then

  1. The CLR calls the NewClass.Write()
  2. The CLR initialises the NewClass.consoleWriter object.
  3. The CLR sees that the implementation lies in BaseClass, but the method is inherited through
  4. The CLR executes the method locally (in NewClass) using the NewClass.consoleWriter variable

I thought this is how the inheritance structure works?

Please can someone help me understand why this is not working?

—

Update :

This scenario would work as follows (how I’ve implemented it)

public class LogBase
{
   protected static TraceSource logger = new TraceSource("");

   public static void Error (string text) { logger.WriteError(text); }
   public static void Info (string text) { logger.WriteInformation(text); }
   public static void Warning (string text) { logger.WriteWarning(text); }
   public static void Verbose (string text) { logger.WriteVerbose(text); }
}

// DataAccess logging
public class DALog : LogBase
{
    protected new static TraceSource logger = new TraceSource("DataAccess");
}

// BusinessObjects logging
public class BOLog : LogBase
{
    protected new static TraceSource logger = new TraceSource("Objects");
}

// BusinessLogic logging
public class BLLog : LogBase
{
    protected new static TraceSource logger = new TraceSource("Logic");
}

// WebUI logging
public class WebUILog : LogBase
{
    protected new static TraceSource logger = new TraceSource("WebUI");
}

The reason being I don’t have to duplicate the code for every single class.

—

Update (after solution chosen):

So in order to get around this problem, instead of using base classes, I defined the functionality one. Then created new classes, which held Singleton instances for each layer:

public sealed class LogBase
{
   private TraceSource logger = null;

   public static LogBase GetLogger(string loggerName) 
   {
       return new LogBase(loggerName);
   }

   private LogBase(string loggerName) { logger = new TraceSource(loggerName); }

   public void Error (string text) { logger.WriteError(text); }
   public void Info (string text) { logger.WriteInformation(text); }
   public void Warning (string text) { logger.WriteWarning(text); }
   public void Verbose (string text) { logger.WriteVerbose(text); }
}

// DataAccess logging - no base class
public class DALog 
{
    private static LogBase logger;

    public static LogBase Instance
    { 
         get 
         { 
              if (logger==null) { logger = new TraceSource("DataAccess"); }
              return logger;
         }
    }
}

...
  • 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-15T07:16:57+00:00Added an answer on May 15, 2026 at 7:16 am

    The new operator does not actually override in the sense of polymorphism. It just adds another method which “happens” to have the same signature and yet is treated as a separate method. Only if you execute that method explicitly on the subclass, the “new” implementation will be used.

    In your case, you have implemented Write only on the base class. In there, you have the line that calls consoleWriter. That line refers to the base class and thus uses the original implementation. It doesn’t even know about the additional implementation in the subclass.

    Review your code and regard it as it would be:

    public class BaseClass
    {
        protected static ConsoleWriter consoleWriter = new ConsoleWriter("");
    
        public static void Write(string text)
        {
            consoleWriter.Write(text);
        }
    }
    
    
    public class NewClass : BaseClass
    {
        protected static ConsoleWriter anotherConsoleWriter = new ConsoleWriter(">");
    }
    

    Your BaseClass.Write wouldn’t ever consider to call anotherConsoleWriter instead of consoleWriter.

    If you want your example to work, you either need to add a new implementation for Write:

    public class NewClass : BaseClass
    {
        protected new static ConsoleWriter consoleWriter = new ConsoleWriter(">");
    
        public static new void Write(string text)
        {
            consoleWriter.Write(text);
        }
    }
    

    … or, what you most probably originally wanted, rather introduce real overriding in the sense of polymorphism by using override instead of new. However, your base class needs to support that by declaring consoleWriter as virtual. Further (as you mentioned in your comment) this only works if you don’t make these methods static.

    You could apply the Singleton pattern instead if you still want static access to your loggers, or have a look to log4net which already solved all these problems for you.

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

Sidebar

Related Questions

I'm reading Thinking in C++ and I'm confused by the new operator. Here is
I'm new to PHP and I'm confused seeing some examples calling a function with
I'm confused by the discussion and advancement both of a new version of HTML
I'm fairly new to the Zend Framework and MVC and I'm a bit confused
I'm confused about Linq to Entities. Is it the new name of Entity Framework
I am confused between the term file modification time and file changed time. Can
in c++ i have following code class Foobar{ public: Foobar * operator()(){ return new
I'm confused (new to java): When implementing the Runnable interface, one must override the
I am new to Java and confused about the garbage collector in Java. What
I'm new to branching in git and I'm a bit confused by the behaviour

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.