C # Learning Diary 02--input and output

Source: Internet
Author: User
After the previous study, has a preliminary understanding of C #, the ability to create a console application in vs2010, remember when I first began to learn C when the teacher taught us, then learn C # input and output.

The input and output of C # is actually referenced by the system-defined console. Type,

Input: Console.ReadLine () (read one line string to end with Enter),

Console.read () (accepts the first character entered from the keyboard and returns its ASCII code value),

Console.readkey () (Waits for the user to press any key to read one character at a time.) );

Output: Console.WriteLine () (the output is displayed on the same line, and the cursor moves to the next line),

Console.Write () (the output is displayed on the same line, the cursor does not wrap)

Below I write a program, let us enter "HC666 Wish you happy National Day!!! "Then output;

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Firstprogram  {      class program      {          static void Main (string[] args)          {             string say = Console.ReadLine ();   Because its return value is of type string, the defined say also has to be string type             Console.WriteLine (say);    Output say class capacity and wrap .... Compare the following output             Console.Write ("AAA");      Output AAA does not wrap and then outputs BBB             console.write ("BBB");     Output is AAABBB                        }}}  

I type HC666 wish you a happy National Day!!! After entering the return, the output result is shown as:

HC666 wish you a happy National Day!!!     HC666 Wish you a happy National Day!!!     AAABBB

Perhaps when you press ENTER, your running window will flash and exit, which is when the program finishes executing the Console.Write ("BBB"), then the statement is not executed and the exit is ended. In fact, used to write programs in C when also met, at the end of the time can add system ("pause"); or write GetChar (); To get the program to pause, I think C # also applies! On the Internet to check the next Console.readkey () really can actually he has other functions, more or to suspend the program. So I added the Console.readkey () to the end.

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Firstprogram  {      class program      {          static void Main (string[] args)          {             string say = Console.ReadLine ();   Because its return value is of type string, the defined say also has to be string type             Console.WriteLine (say);    Output say class capacity and wrap .... Compare the following output             Console.Write ("AAA");      Output AAA does not wrap and then outputs BBB             console.write ("BBB");     Output is aaabbb             console.readkey ();      Wait for Input}}}  

Sure enough, when the program output AAABBB, the cursor stops behind. At the beginning. Console.readkey () is able to read the first key entered by the user and whether to display this key (the default is to display the key) Console.readkey (true) does not display Console.readkey () or Console.readkey (false), so I added some of the above code requirements do not display the user pressed keys, but then output this button;

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Firstprogram  {      class program      {          static void Main (string[] args)          {               string say = Console.ReadLine ();   Because its return value is of type string, the defined say also has to be string type             Console.WriteLine (say);    Output say class capacity and wrap .... Compare the following output             Console.Write ("AAA");      Output AAA does not wrap and then outputs BBB             console.write ("BBB");     Output is aaabbb             Console.WriteLine ();               Char a= Console.readkey (TRUE). KeyChar;  Assigns a key to a            Console.WriteLine (a) in character form;                Console.readkey ();   Wait for user input            }      }}  

I repeat the above input HC666 wish you a happy National Day!!! Enter and press the "a" key without the carriage return to execute down, because it only allows to enter a character, the result is:

HC666 wish you a happy National Day!!!     HC666 Wish you a happy National Day!!!     aaabbb     A

I said Console.readkey () mostly used to suspend the program, in fact, it's not right, I checked it there are many other features such as String ch = console.readkey (). key.tostring; Convert the key to string type, think of string type I am excited, with it can be fast and concise character processing, this point of learning C + + students are very clear, and then carefully study. If using Readkey () as the end of the pause is absolutely no need to do so, vs2010 can do it, each time you run the program do not click the green triangle but debug--〉 start (not debugging) Click can be done.

Finally Console.read () it and Consle.readkey () think more than the former can output more than one character, but only take the first character, and return its ASCII code value, this we can query the ASCII code of a character

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Firstprogram  {      class program      {          static void Main (string[] args)          {             int a= Console.read ();             Console.WriteLine (a);}}  

I entered the 1234 return car in the Run window results for:

123449 1 of the ASCII 49 is consulted.

In-depth exploration of 01:

In the previous article, VS has automatically written a series of using system when we automatically generate project files for us .... I said it was actually quoted as a predefined type of system, in the spirit of the carbon of lifelong learning I deleted them all, so that the program can still run? In contrast to C, if we write a program in C or C + + to turn the file, the program return error can not run, C # is based on C + + is also higher than it, so it is able to run only need ...

Namespace Firstprogram  {      class program      {          static void Main (string[] args)          {             int a= System.Console.Read ();             System.Console.WriteLine (a);}}  

Because the console. is defined in the System namespace, the reference at the beginning of the encoding avoids subsequent repeated calls, so you can delete its references but you must call the namespace each time the method is called.

The above is the C # learning diary 02--input and output content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.