C# -- Console.Read() v.s. Console.ReadKey() v.s. Console.ReadLine()

C# -- Console.Read() v.s. Console.ReadKey() v.s. Console.ReadLine() 


Console.ReadKey():

[syntax]

 public static ConsoleKeyInfo Console.ReadKey(bool key=false);

[description]

Read a character from your keyboard.

[argument]

key:

By Default, key is set to be false.

When arg. key was not given or key==false , Console.ReadKey() will NOT display in Console. 

Otherwise, when key==true. It will display in Console.

[return type]

ConsoleKeyInfo


[usage]

(1)not continue the process until the user press any key.

I can write the following code

 Console.ReadKey();


(2)not continue the process until the user press special key.

using while() loop.

If I wanna to enter 'E' to continue the process. I can write the following code:

while( Console.ReadKey().Key!=ConsoleKey.E){}


Console.Read():

[syntax]

[System.Runtime.Versioning.UnsupportedOSPlatform("browser")] public static int Read ();

[description]

return next character from instream (i.e. input buffer stream)

[return type]

Int32

[resource]

Microsoft website:

Console.Read 方法 (System) | Microsoft Docs


Console.ReadLine():

[syntax]

[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]

public static string? ReadLine ();

[description]

return next line from instream (i.e. input buffer stream).

if there is no next line , it will return null.

[return type]

String

[resource]

Microsoft website:

Console.Read 方法 (System) | Microsoft Docs


[NOTE]

For more details about '?' question mark , you can take a look at the following page:

Although the microsoft documentation didn't explain about the meaning of  '?' , I think I can understand the meaning of '?' from the example of following page.

?? and ??= operators - C# reference | Microsoft Docs

Comments

Popular posts from this blog

String Interpolation(字串內插)

Format a string -- String.Format()