在线运行 C#
在浏览器中编写、编译并运行 C# 代码,适合学习、练习与快速验证。
C# 代码编辑器
输出
标准输出(stdout)
标准错误(stderr)
执行信息
退出码:
运行状态:
使用说明
- 在左侧编辑器编写 C# 代码(包含
static void Main(string[] args)入口)。 - 点击“运行代码”即可在线编译并执行程序。
- 右侧面板展示输出与错误信息。
- 绿色区域为标准输出(例如
Console.WriteLine输出)。 - 红色区域为编译/运行错误与警告。
- 执行信息包含退出码与运行状态。
- 快捷键:
Ctrl+Enter(Mac 上为Cmd+Enter)。
C# 基础
Hello World:
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, C#!");
}
}
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, C#!");
}
}
常用类型:
int/long/double/boolstring/char- 集合:
int[]、List<T>、Dictionary<K,V>
控制结构
条件与循环:
int n = 5;
if (n % 2 == 0) Console.WriteLine("even"); else Console.WriteLine("odd");
for (int i = 0; i < 3; i++) Console.WriteLine(i);
if (n % 2 == 0) Console.WriteLine("even"); else Console.WriteLine("odd");
for (int i = 0; i < 3; i++) Console.WriteLine(i);
方法与集合
示例:
using System;
using System.Linq;
class Program {
static int Add(int a, int b) => a + b;
static void Main(string[] args) {
Console.WriteLine(Add(2,3));
int[] nums = new int[] {3,7,1,9,4};
Console.WriteLine(nums.Max());
}
}
using System.Linq;
class Program {
static int Add(int a, int b) => a + b;
static void Main(string[] args) {
Console.WriteLine(Add(2,3));
int[] nums = new int[] {3,7,1,9,4};
Console.WriteLine(nums.Max());
}
}
示例程序(点击上方运行)
1. 递归计算阶乘
using System;
class Program {
static long Fact(int n) => n <= 1 ? 1 : n * Fact(n-1);
static void Main(string[] args) { Console.WriteLine($"5! = {Fact(5)}"); }
}
class Program {
static long Fact(int n) => n <= 1 ? 1 : n * Fact(n-1);
static void Main(string[] args) { Console.WriteLine($"5! = {Fact(5)}"); }
}
2. 数组最大值
using System;
using System.Linq;
class Program {
static void Main(string[] args) {
int[] nums = new int[] {3,7,1,9,4};
Console.WriteLine($"Maximum: {nums.Max()}");
}
}
using System.Linq;
class Program {
static void Main(string[] args) {
int[] nums = new int[] {3,7,1,9,4};
Console.WriteLine($"Maximum: {nums.Max()}");
}
}