The specific usage of the C # console class _c# Tutorial

Source: Internet
Author: User
Tags readline

Console.Write represents writing a string directly to the console without wrapping it and continuing to write the preceding character.
Console.WriteLine indicates that the string is written to the console and then wraps.
Console.read indicates that the string is read from the console without wrapping.
Console.ReadLine represents a newline after reading a string from the console.
Console.readkey gets the next character or function key pressed by the user, and the pressed key appears in the console window.
Console.beep play the Beep through the console speaker.
Console.clear clears the console buffer and the display information for the corresponding console window.

Output to console

The output to the console is to output the data to the console and display it. NET Framework provides the console class for this task, and the output is as follows:

Console.WriteLine ();
Console.Write ();
Console.WriteLine (value of output);
Console.Write (value of output);
Console.WriteLine ("format string for Output", List of variables);
Console.Write ("format string for Output", List of variables);

The only thing that Console.wrietline () and Console.Write () is the one that is out of line and the other does not.
Console.WriteLine ("The wife of {0} in the Deer ding Kee has {1},{2},{3} and so on 7", Strname[0],strname[1],strname

[2],strname3]);
This method contains two parameters: the format string and the list of variables. "The wife of {0} in the deer tripod has {1},{2},{3} and so on 7" This is a format string, {0}, {1}, {2}, {3}, are called placeholders, represent the variable table that follows, 0 corresponds to the first variable in the list of variables, 1 corresponds to the 2nd variable in the list of variables, and so on, complete the output.

Enter from console

Input methods provided by the console class:

Console.ReadLine ();

This code returns a string of data that can be assigned directly to a string variable, such as:
String Strname=console.readline ();
Sometimes you need to enter a number from the console and use the content described earlier to convert data, such as:
int Num=int. Pares (Console.ReadLine ());
int Num=convert.toint32 (Console.ReadLine ());
The above two lines of code effect the same, you can choose any one according to their own habits.

Attention:

The input results of console.readline () and Console.read () are completely different and cannot be mixed.
Console.read (), returning the ASCII code of the value-led character
Console.ReadLine (), the return value is a string
That is, the Read method can read only the first character, and ReadLine can read more than one character .

The role of Console.readkey (), read is reading from the console, key means to press the keyboard, then the combination means to get the user presses the function key to display in the window, used in the previous code to suspend the window function, in the debug state, only press any key after the window will be closed.

Console input and output

Copy Code code 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 ("Please enter the names of two students");
String Name1=console.readline ();
String Name2=console.readline ();
Console.WriteLine ("Please input two student's score");
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 ();
}
}
}

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.