Beginner question ahead.
If I want to convert input string (from console) to uppercase the syntax is the following one:
string text = Console.ReadLine().ToUpper();
Since "ToUpper()" is a method, I'm confused as to why console input is not its parameter?
string text = String.ToUpper(Console.ReadLine());
CodePudding user response:
Because ToUpper
is declared like this according to the documentation:
public string ToUpper ();
Notice that it has no static
modifier. This means that you should call it on an instance of a string, like the value returned by Console.ReadLine()
, rather than on the class string
. Also note that it takes no parameters (empty parentheses).
If it were declared like this instead:
public static string ToUpper (string s);
Then your way of calling it, string.ToUpper(Console.ReadLine())
. In this case, it is static
, so you should call it on the string
class - the class in which it is declared, and it also takes a string
parameter, which you have passed Console.ReadLine()
.
CodePudding user response:
There is no overload ToUpper()
method that reads string as an argument and it is not even static method to use it like String.ToUpper()
.
There are only two overload methods that convert a string into upper.
ToUpper()
: Returns a copy of this string converted to uppercase.ToUpper(CultureInfo)
: Returns a copy of this string converted to uppercase, using the casing rules of the specified culture.