Instructions for using the C # console class in asp.net

Source: Internet
Author: User

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:

The code is as follows Copy Code
Console.Write (output content);

Indicates that the specified content is written directly to the console without line wrapping. For different data types, this method has more than n overloaded implementations.

The code is as follows Copy Code
Console.WriteLine (output content);

Indicates that the string is being written to the console and then wrapped. The method also has multiple overloaded implementations, which are used to output different types of data.

The code is as follows Copy Code
Console.WriteLine ("format string for Output", List of variables);

The method contains two parameters: a format string and a list of variables that can be used when multiple variables require output.

Example:

Console.WriteLine ("B minor Rain" by {0} lyrics, by {1} singing. "," Xiaosong "," Ye Be "), where {0}, {1} is called a placeholder, represents the variable table in the following order, the first variable in the corresponding variable list, the 2nd variable in the corresponding variable list, and so on, completes the output.

Enter from console

Input methods provided by the console class:

The code is as follows Copy Code
Console.ReadLine ();

This method reads a row of data from the console and can assign it directly to a string variable, such as:

The code is as follows Copy Code
String Strname=console.readline ();

Sometimes you need to enter numbers from the console, you need to convert data, such as:

The code is as follows Copy Code
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.

Console.read () returns the ASCII code of the value-led character

Console.ReadLine () return value is string

That is, the Read method can read only the first character, and ReadLine can read more than one character or wrap it.

Note: The input results for console.readline () and Console.read () are completely different and cannot be mixed.

The following is a comprehensive example:

  code is as follows copy code

Using System;
Namespace consoleone{
Class consolestudy{
public static void Main () {
Console.WriteLine ("Please enter the names of two students"); br> string Name1=console.readline ();
String Name2=console.readline ();
Console.WriteLine ("Please enter two student's results");
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.