运行你的第一个 C# 程序
Coddy 面向 C++ 开发者的 C# 课程的第 1 课,共 48 课。
C++ 编译成本机代码,并启动全局 main;这门 C# 课程使用 mcs 编译已声明的类,并在 Mono 下启动静态 Main。C# 会管理对象内存,但资源生命周期仍需要明确设计。
C# 是 .NET 系列中经过静态检查的语言。这门课程使用兼容的 Mono 编译器,以及显式声明的类和方法。你不需要使用现代记录、顶层语句或可空引用注解。先成功运行一次;接下来的课程会讲解语法和程序结构。
C++ 参考
#include <algorithm>
#include <array>
#include <cctype>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
int main() {
cout << "C# is ready" << "\n";
}
Main 中的 C# 语句
Console.WriteLine("C# is ready");这些是重点教学片段。挑战文件包含完整的可执行包装代码;上面的代码片段展示了相关的声明和语句。
参考输出:
C# is readyC# 输出:
C# is ready挑战
简单按原样运行提供的程序。没有输入。它会打印 C# is ready。后续任务会将你可编辑的代码放在一个与可见的锁定启动器分开的 C# 源文件中。
编辑未锁定的 C# 文件。可见的锁定文件 Main.cs 会读取所述行,调用 Exercise.Solve,验证输入未被修改,并在末尾打印一个换行符。保留已声明的 API。不要添加第二个 Main,也不要打印诊断信息。
亲手试试
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var result = Exercise.Solve();
Console.WriteLine(result);
}
}
面向 C++ 开发者的 C#中的所有课
1从这里开始
运行你的第一个 C# 程序自己练习: C# 在线编译器