Overloading Console.ReadLine possible? (or any static class method)
Posted
by
comecme
on Stack Overflow
See other posts from Stack Overflow
or by comecme
Published on 2010-12-22T09:47:06Z
Indexed on
2010/12/22
9:54 UTC
Read the original article
Hit count: 198
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?
© Stack Overflow or respective owner