wjh0312 2023-03-02 10:08 采纳率: 100%
浏览 28
已结题

C# mono的可为null变量如何处理?

我写了如下代码:

using System;

class Program {

    static int n, m;
    static int[]? f = null;

    static int find(int x) {
        if (f![x] == x) return x;
        return f[x] = find(f[x]);
    }

    static void merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x != y) f![x] = y;
    }

    static void Main(String[] args) {
    }
}

f 在定义时是 null,在 Main 中分配 f = new int[n + 1];

但是这段代码在 洛谷IDE 中的C# mono中无法编译通过。

报错如下

/tmp/compiler_hlxsun_w/src(16,17): error CS1519: Unexpected symbol `?' in class, struct, or interface member declaration
/tmp/compiler_hlxsun_w/src(16,21): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
/tmp/compiler_hlxsun_w/src(19,13): error CS1525: Unexpected symbol `!'
/tmp/compiler_hlxsun_w/src(19,14): error CS1525: Unexpected symbol `['
/tmp/compiler_hlxsun_w/src(26,21): error CS1525: Unexpected symbol `!'
/tmp/compiler_hlxsun_w/src(26,22): warning CS0642: Possible mistaken empty statement
/tmp/compiler_hlxsun_w/src(26,22): error CS1525: Unexpected symbol `['
Compilation failed: 6 error(s), 1 warnings

我查阅了一些资料也没有什么发现,想请问有没有C# mono的相关资料?或者这部分应该如何写在visual studio 的C
#和C# mono中都没有warning和error?

  • 写回答

3条回答 默认 最新

  • 全栈小5 优质创作者: 编程框架技术领域 2023-03-02 10:17
    关注

    查看下版本是多少

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月2日
  • 已采纳回答 3月2日
  • 创建了问题 3月2日