// C# 基础示例代码
using System;
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            // 输出 "Hello, World!" 到控制台
            Console.WriteLine("Hello, World!");
            // 定义一个变量并赋值
            int number = 10;
            Console.WriteLine("The number is: " + number);
            // 使用 if-else 语句进行条件判断
            if (number > 5)
            {
                Console.WriteLine("Number is greater than 5");
            }
            else
            {
                Console.WriteLine("Number is less than or equal to 5");
            }
            // 使用 for 循环
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Iteration " + i);
            }
            // 使用数组
            string[] names = { "Alice", "Bob", "Charlie" };
            foreach (string name in names)
            {
                Console.WriteLine("Name: " + name);
            }
            // 定义和调用方法
            int sum = AddNumbers(3, 7);
            Console.WriteLine("Sum: " + sum);
        }
        // 定义一个简单的加法方法
        static int AddNumbers(int a, int b)
        {
            return a + b;
        }
    }
}using System; 引入了系统命名空间,namespace HelloWorld 定义了一个命名空间,class Program 定义了一个类。int number = 10; 定义了一个整数类型的变量 number 并赋值为 10。if-else 语句用于根据条件执行不同的代码块。for 循环用于重复执行一段代码,foreach 循环用于遍历数组或集合。AddNumbers 是一个自定义方法,接收两个整数参数并返回它们的和。这段代码展示了 C# 中的基本语法和常用结构。
上一篇:c# %
下一篇:c#属性
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站