C # usage of the Console class

Source: Internet
Author: User

Console. Write: directly writes strings to the Console without line breaks. You can continue writing the previous characters.
Console. WriteLine indicates to write a string to the Console and then wrap it.
Console. Read indicates reading strings from the Console without line breaks.
Console. ReadLine indicates reading strings from the Console and wrapping them.
Console. ReadKey gets the next character or function key that the user presses. The pressed key is displayed in the Console window.
Console. Beep play the prompt sound through the Console speaker.
Console. Clear clears the Console Buffer and the display information of the corresponding Console window.

Output to console

Output to the console is to output the data to the console and display it .. The Net Framework provides the console class to implement this task. The output method is as follows:

Console. WriteLine ();
Console. Write ();
Console. WriteLine (output value );
Console. Write (output value );
Console. WriteLine ("output format string", variable list );
Console. Write ("output format string", variable list );

The only difference between Console. WrietLine () and Console. Write () is that the former outputs a new line while the latter does not.
Console. WriteLine ("{0}'s wives in the deer Ding note include {1}, {2}, {3}, and other 7", strName [0], strName [1], strName

[2], strName3]);
This method contains two parameters: "Format String" and Variable list. "{0}'s wife in the deer Ding note has seven {1}, {2}, and {3}." This is a format string, {0}, {1}, {2}, and {3} are placeholders. They represent the variable tables listed in sequence. 0 corresponds to the first variable in the Variable list, 1 corresponds to the 2nd variables in the Variable list, and so on.

Enter

The Console class provides the following input methods:

Console. ReadLine ();

This code returns a string type data, which can be directly assigned to a string variable, such:
String strname = Console. ReadLine ();
Sometimes you need to enter a number in the console to use the preceding content for data conversion, such:
Int num = int. Pares (Console. ReadLine ());
Int num = Convert. ToInt32 (Console. ReadLine ());
The above two statements have the same effect. You can select either of them based on your habits.

Note:

The input results of Console. ReadLine () and Console. Read () are completely different and cannot be mixed.
Console. Read (), return the ASCII code of the first character
Console. ReadLine (), returns a string
That is to say, the read method can only read the first character, while ReadLine can read multiple characters and can also read line breaks.

Console. readKey () is used to read data from the console. The key indicates that the keyboard is pressed. The combination means that the user presses the function key and is displayed in the window, it is used in the previous code to suspend the window. In the debugging status, the window is closed only when any key is pressed.

Console Input and Output

Copy codeThe Code is as follows: using System;
Using System. Collection. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleTest
{
Class ConsoleTest
{
Static void Main (string [] args)
{
Console. WriteLine ("enter the names of two students ");
String name1 = Console. ReadLine ();
String name2 = Console. ReadLine ();
Console. WriteLine ("Enter the scores of two students ");
Int score1 = int. Parse (Console. ReadLine ());
Int score2 = int. Parse (Console. ReadLine ());
Console. WriteLine ("first student's name {0}, score {1}", name1, score1 );
Console. WriteLine ("second student's name {0}, score {1}", name2, score2 );
Console. ReadKey ();
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.