search
HomeBackend DevelopmentC#.Net TutorialC# Learning Diary 02--Input and Output

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  
              
        }  
    }  
}

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

 HC666祝你国庆快乐!!!
     HC666祝你国庆快乐!!!
     aaabbb

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();      //等待输入  
              
        }  
    }  
}

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();   //等待用户输入  
  
        }  
    }  
}

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

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);  
        }  
    }  
}

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

1234
49
经过查阅1的ASCII 为49。

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);  
        }  
    }  
}

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 (www.php.cn)!

Statement
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
C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# .NET: Exploring Core Concepts and Programming FundamentalsC# .NET: Exploring Core Concepts and Programming FundamentalsApr 10, 2025 am 09:32 AM

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft