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

The Archive Base Latest Questions

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

I’m trying to create an overload of the System.Console.ReadLine() method that will take a

  • 0

I’m trying to create an overload of the System.Console.ReadLine() method that will take a string argument. My intention basically is to be able to write

string s = Console.ReadLine("Please enter a number: ");

in stead of

Console.Write("Please enter a number: ");
string s = Console.ReadLine();

I don’t think it is possible to overload Console.ReadLine itself, so I tried implementing an inherited class, like this:

public static class MyConsole : System.Console
{
    public static string ReadLine(string s)
    {
        Write(s);
        return ReadLine();
    }
}

That doesn’t work though, cause it is not possible to inherit from System.Console (because it is a static class which automatically makes is a sealed class).

Does it make sense what I’m trying to do here? Or is it never a good idea to want to overload something from a static class?

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

    Just wrap the console in your own class and use that instead. You don’t need to inherit for that:

    class MyConsole
    {
        public static string ReadLine()
        {
            return System.Console.ReadLine();
        }
        public static string ReadLine(string message)
        {
            System.Console.WriteLine(message);
            return ReadLine();
        }
        // add whatever other methods you need
    }
    

    Then you can go ahead and use that one in your program instead:

    string whatEver = MyConsole.ReadLine("Type something useful:");
    

    If you wish to push it a bit further and make MyConsole a bit more flexible, you could also add support to replace the input/output implementations:

    class MyConsole
    {
        private static TextReader reader = System.Console.In;
        private static TextWriter writer = System.Console.Out;
    
        public static void SetReader(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            MyConsole.reader = reader;
        }
    
        public static void SetWriter(TextWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            MyConsole.writer = writer;
        }
    
    
        public static string ReadLine()
        {
            return reader.ReadLine();
        }
        public static string ReadLine(string message)
        {
    
            writer.WriteLine(message);
            return ReadLine();
        }
        // and so on
    }
    

    This would allow you to drive the program from any TextReader implementation, so commands could come from a file instead of the console, which could provide some nice automation scenarios…

    Update
    Most of the methods that you need to expose are extremely simple. OK, a bit tedious to write perhaps, but it’s not many minutes of work, and you need to do it only once.

    Example (assuming we are in my second sample above, with assignable reader and writer):

    public static void WriteLine()
    {
        writer.WriteLine();
    }
    
    public static void WriteLine(string text)
    {
        writer.WriteLine(text);
    }
    
    public static void WriteLine(string format, params object args)
    {
        writer.WriteLine(format, args);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.