Home > Backend Development > C#.Net Tutorial > C# Learning Diary 02--Input and Output

C# Learning Diary 02--Input and Output

黄舟
Release: 2017-01-20 13:17:35
Original
1782 people have browsed it

After studying the previous article, I already have a preliminary understanding of C# and can create a console application in vs2010. I remember that the teacher taught us this way when I first started learning C. Next, let’s learn the input and output of C#.

The input and output of C# actually refer to the Console. type defined by System.

Input: Console.ReadLine() (reading a line of String type string ends with the Enter key) ,

Console.read () (Accept the first character entered from the keyboard, and return its ascii code value),

console.readkey () (Waiting for the user to press the any key , read one character at a time. ); (The output content is displayed on the same line, the cursor does not wrap)

Next I will write a program, let us enter "HC666 wishes you a 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();   //因为它的返回值是String类型的所以定义的say也得是String类型  
           Console.WriteLine(say);    //输出say的类容并换行。。。。对比以下的输出  
           Console.Write("aaa");      //输出aaa不换行接着输出bbb  
           Console.Write("bbb");     //输出为aaabbb  
              
        }  
    }  
}
Copy after login

I type HC666 wishes you a happy National Day! ! ! After entering Enter again, the output result is displayed as:

 HC666祝你国庆快乐!!!
     HC666祝你国庆快乐!!!
     aaabbb
Copy after login

Maybe when you press Enter, your running window will flash by and then exit. This should be because the Console is running after the program is executed. When .Write("bbb");, there is no statement to execute, so it ends and exits. In fact, I have encountered this when writing programs in C before. At that time, you could add System("pause"); or write getchar(); at the end to make the program pause. I think C# is also applicable! I checked the Internet and found that Console.ReadKey() works. In fact, it has other functions, most of which are used to pause the program. So I added Console.ReadKey() at 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();   //因为它的返回值是String类型的所以定义的say也得是String类型  
           Console.WriteLine(say);    //输出say的类容并换行。。。。对比以下的输出  
           Console.Write("aaa");      //输出aaa不换行接着输出bbb  
           Console.Write("bbb");     //输出为aaabbb  
           Console.ReadKey();      //等待输入  
              
        }  
    }  
}
Copy after login

Sure enough, when the program outputs aaabbb, the cursor stops behind it. At the beginning, we said that Console.ReadKey() can 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) is displayed, so I added some requirements to the above code not to display the key pressed by the user, but then output this key;

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();   //因为它的返回值是String类型的所以定义的say也得是String类型  
           Console.WriteLine(say);    //输出say的类容并换行。。。。对比以下的输出  
           Console.Write("aaa");      //输出aaa不换行接着输出bbb  
           Console.Write("bbb");     //输出为aaabbb  
           Console.WriteLine();  
             char a= Console.ReadKey(true).KeyChar;  //将按键以字符形式赋值给a  
          Console.WriteLine(a);      
          Console.ReadKey();   //等待用户输入  
  
        }  
    }  
}
Copy after login

I repeat the above input HC666 wishes you a happy National Day! ! ! Enter and then press the "A" key to execute the program without entering, because it only allows one character to be entered, and the result is:

HC666祝你国庆快乐!!!
     HC666祝你国庆快乐!!!
     aaabbb
     A
Copy after login

I said before that Console.ReadKey() is mostly used It's actually wrong to pause the program. I checked it and it has many other functions, such as String ch = Console.ReadKey().Key.ToString; to convert keys into String type. I was excited when I thought of String type. I used it. It can perform fast and concise character processing. Students who have studied C++ know this very well. We will study it carefully in the future. If you use ReadKey() as the pause at the end, there is no need to do this. vs2010 can do it by itself. Do not click the green triangle every time you run the program but debug -> start execution (without debugging) Just click and you're done.

Finally, Console.Read() and Consle.ReadKey() can output more than one character, but only take the first character and return its ASCII code value. We can query a character The ASCII code

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);  
        }  
    }  
}
Copy after login

I entered 1234 in the run window and pressed Enter. The result is:

1234
49
经过查阅1的ASCII 为49。
Copy after login

In-depth exploration of 01:

Previous article China VS automatically wrote a series of using system when automatically generating project files for us.... I said that it actually refers to the types predefined by the system. In the spirit of endless learning, I deleted them all. , can the program still run in this case? Compared with C, if we remove the header file when writing a program in C or C++, the program will report an error and cannot run. C# is based on C/C++ and is higher than it, so it can run and only needs...

namespace FirstProgram  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
           int a= System.Console.Read();  
           System.Console.WriteLine(a);  
        }  
    }  
}
Copy after login

Because Console. is defined in the System namespace, referencing it at the beginning of the encoding avoids repeated calls later, so its reference can be deleted but the namespace must be called every time the method is called.

The above is the content of C# Learning Diary 02--Input and Output. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template