← 都来码 / Dolaima← 例程索引csharp/school/MANIFEST.md 原文

csharp/school/MANIFEST.md · 5876 字节 · 下载原文

# C# 外部老程序语料(10 个)

抓取日:2026-10-14 · 编译判据:`scripts/vml-compat-external.sh csharp`(只看**编不编得过**)

**结果:通过 3 / 失败 7 / 未取证 0。**
机械口径(汇编 / 硬件地址 / 界面库):`python3 scripts/compat-external-lint.py --dir <本目录>` ⇒ **10 个文件 0 处违规**。

## 来源与许可

| 来源 | 说明 | 许可 |
|:-----|:-----|:-----|
| Rosetta Code | 页面 wikitext(`?action=raw`)里的 `=={{header\|C sharp}}==` 段,取第一个含 `Main` 的 `<syntaxhighlight>` 块,**逐字节未改** | **GFDL 1.2**(站点版权页 `Rosetta_Code:Copyrights` 明示;贡献者可另选更宽松许可,逐页未标注) |
| raspberrypilearning/ada-code-samples | Raspberry Pi Foundation 的 Ada Computer Science 教材例程 | **CC BY-NC-SA 4.0**(文件头逐条写明) |

⚠ Rosetta Code 的页面分类里写着 `Classic CS problems and programming tasks` —— 这批正是该语言传统上被当作练习的那类程序。

## 清单

| 文件名 | 类别 | 来源 URL | 许可 | 行数 | 编译 | 第一条错误(原文) |
|:-------|:-----|:---------|:-----|-----:|:----:|:-------------------|
| `ackermann.cs` | 数值/递归 | https://rosettacode.org/wiki/Ackermann_function | GFDL 1.2 | 32 | ✅ 通过 | — |
| `factorial.cs` | 数值/算术 | https://github.com/raspberrypilearning/ada-code-samples/blob/main/recursion/recursion-examples/c-sharp/factorial.cs | CC BY-NC-SA 4.0 | 40 | ❌ | `factorial.cs:25:31: error: 期望 ')' 在实参后`(第 25 行 = `Console.WriteLine($"{n}! is: {result}");`,插值字符串) |
| `substring_demo.cs` | 字符串/文本 | https://github.com/raspberrypilearning/ada-code-samples/blob/main/string-handling/substrings/c-sharp/substring_demo.cs | CC BY-NC-SA 4.0 | 42 | ✅ 通过 | — |
| `string_case.cs` | 字符串/文本 | https://rosettacode.org/wiki/String_case | GFDL 1.2 | 23 | ❌ | `csharp错误: string_case.cs: 内部错误: Object reference not set to an instance of an object.`(**编译器自己崩了**,没有行号) |
| `bubble_sort.cs` | 数组/排序 | https://github.com/raspberrypilearning/ada-code-samples/blob/main/sorting-algorithms/bubble-sort/c-sharp/bubble_sort_version_1.cs | CC BY-NC-SA 4.0 | 57 | ❌ | `bubble_sort.cs:31:32: error: 未声明的变量 'testItems' [CodeGen_UndefinedVariable]`(**根源在第 26 行** `int[] testItems = new int[] {43, 21, …};` —— 解析器把整条声明**静默吞掉**,报的是 5 行之后的**使用点**) |
| `binary_search.cs` | 数组/搜索 | https://rosettacode.org/wiki/Binary_search | GFDL 1.2 | 43 | ❌ | `binary_search.cs:25:25: error: 期望 ')' 在实参后`(第 25 行 = `Console.WriteLine($"test[{index}]: {join}");`,插值字符串) |
| `bottles_of_beer.cs` | 控制流 | https://rosettacode.org/wiki/99_bottles_of_beer | GFDL 1.2 | 29 | ✅ 通过 | — |
| `n_queens.cs` | 递归/回溯 | https://rosettacode.org/wiki/N-queens_problem | GFDL 1.2 | 43 | ❌ | `n_queens.cs:14:58: error: 期望 ')' 在方法实参后`(第 14 行 = `cols.Combinations(2).Select(pairs=> pairs.ToArray());`,LINQ + lambda) |
| `guess_the_number.cs` | 经典小游戏 | https://rosettacode.org/wiki/Guess_the_number | GFDL 1.2 | 19 | ❌ | `guess_the_number.cs:13:17: error: 期望 ')' 在 if 条件后`(第 13 行 = `if (int.Parse(Console.ReadLine()) == randomNumber)`) |
| `monty_hall.cs` | 经典模拟 | https://rosettacode.org/wiki/Monty_Hall_problem | GFDL 1.2 | 36 | ❌ | `<input>:16: error: 未定义的函数 'Next'(引用 3 次)`(第 16 行 = `var winner = gen.Next(3);`;同批还有 `<input>:33: error: 未定义的函数 'Console_Out_WriteLine'(引用 2 次)`) |

## 失败的 6 种根因(按出现频次)

链路只管"编不编得过",以下是逐条**人工看回源码**归的因(不是脚本判的):

1. **`$"…"` 插值字符串** —— 报 `期望 ')' 在实参后`。命中:`factorial.cs`、`binary_search.cs`。(C# 6.0,2015)
2. **`new int[] { … }` 显式数组初始化器** —— 声明被静默吞掉、报**后面**的使用点 `未声明的变量`。命中:`bubble_sort.cs`(同类还有 `new int[][] { new int[] {…}, … }` 的锯齿数组)。(`int[] a = {…}` 与 `new int[5]` 都正常,只有这个形态坏)
3. **`Console` 以外的成员调用被当成"自由函数"** —— 前端只认识 `Console.Write/WriteLine`;其余一律发射成 `<接收者>_<方法名>` 的调用,链接期报 `未定义的函数`。实测标签:`Next`、`Console_Out_WriteLine`、`nameof`、`Convert_ToString`、`i_ToString`、`value_ToCharArray`、`str_ToCharArray`。**这是本批最高频的一条**(连同 4 一起占失败的大半)。
4. **`int.Parse` / `char.IsLetter` / `char.IsUpper` / `Random` 这类"类型名.方法"** —— 报 `期望 ')' 在 if 条件后` / `未声明的变量 'ch'`。`int.Parse` 即使不在 `if` 里也编不过(`int n = int.Parse("42");` ⇒ `未声明的变量 'n'`)。
5. **LINQ / 泛型集合**(`List<int>`、`System.Linq`、`.Select/.Where/.Aggregate`、lambda)+ 锯齿数组 —— 命中 `n_queens.cs`。(题面已预告)
6. **编译器内部崩溃**(`NullReferenceException`)—— 命中 `string_case.cs`。最小复现(已收窄到 4 行):

   ```csharp
   class T { static void Main() { f("a"); }
     static void f(string str) { string n = ""; foreach (char i in str) if (char.IsLower(i)) n += char.ToUpper(i); else n += char.ToLower(i); Console.WriteLine("C: {0}", n); } }
   ```
   ⇒ `内部错误: Object reference not set to an instance of an object.`
   去掉 `else` 那半 ⇒ 退化成普通报错(`未声明的变量 'n'`);去掉 `char.Is*` ⇒ 也退化成普通报错。**这是本批最该先修的一条:产品口径是"允许出错、不允许闪退"。**