using System;
namespace VisualCSharpExample
{
class Program
{
static void Main(string[] args)
{
// 这是一个简单的控制台应用程序,用于演示Visual C#的基本用法。
Console.WriteLine("Hello, Visual C#!");
// 定义一个变量并输出它的值
int number = 42;
Console.WriteLine("The number is: " + number);
// 使用条件语句
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
// 循环示例
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Iteration: " + i);
}
// 调用方法
PrintMessage("This is a method call.");
// 等待用户输入后退出
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
// 定义一个方法
static void PrintMessage(string message)
{
Console.WriteLine(message);
}
}
}
namespace VisualCSharpExample 和 class Program 定义了一个命名空间和一个类。命名空间用于组织代码,类是程序的基本构建块。static void Main(string[] args) 是程序的入口点。每个 C# 控制台应用程序都必须有一个 Main 方法。int number = 42; 定义了一个整数类型的变量 number 并赋值为 42。if, else if, 和 else 来根据条件执行不同的代码块。for 循环用于重复执行一段代码多次。PrintMessage("This is a method call."); 调用了一个自定义方法 PrintMessage。Console.ReadKey(); 用于等待用户按键,防止控制台窗口立即关闭。这个示例展示了如何使用 Visual C# 编写一个简单的控制台应用程序,并涵盖了基本的语法和结构。
上一篇:c#回调函数
下一篇:c# 字符串补零
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站