📜  c# print out - C# (1)

📅  最后修改于: 2023-12-03 14:39:43.832000             🧑  作者: Mango

C# Print Out

C# is a popular programming language used for creating desktop applications, web applications, games, and more. One essential feature of any programming language is the ability to print out information to the user. C# provides several ways to do this.

Console.WriteLine

The Console.WriteLine method is used to print out text and values to the console, which is the black window that appears when running a console application. It takes a string or object as a parameter and prints it to the console.

Console.WriteLine("Hello, world!");

The above code will print the text "Hello, world!" to the console.

You can also use Console.WriteLine with multiple parameters to print out multiple strings and values in one line.

int age = 25;
string name = "John";
Console.WriteLine("My name is {0} and I am {1} years old.", name, age);

This will print out "My name is John and I am 25 years old." to the console.

Console.Write

The Console.Write method works similarly to Console.WriteLine, but does not add a new line after printing. This means that consecutive Console.Write statements will print out on the same line.

Console.Write("Hello, ");
Console.Write("world!");

This will print out "Hello, world!" on the same line.

String Interpolation

C# 6.0 introduced a new feature called string interpolation, which makes it easier to format strings with multiple values.

int age = 25;
string name = "John";
Console.WriteLine($"My name is {name} and I am {age} years old.");

This code is equivalent to the previous Console.WriteLine example using the "placeholder" syntax.

Conclusion

C# provides several ways to print out information to the user, including Console.WriteLine, Console.Write, and string interpolation. These methods are essential for debugging and user interaction in C# applications.