c# - C# 中的 Typedef 等价物?

标签 c# syntax typedef

我知道这个问题 has been asked ,但那里发布的解决方案似乎对我不起作用。这是我正在尝试的代码:

namespace ConsoleApplication5
{
    class Program
    {
        enum Tile { Empty, White, Black };
        using Board = Tile[8,8];

我得到的错误:

Invalid token 'using' in class, struct, or interface member declaration

似乎“using”子句必须移到 Program 类之外,但那里不存在我的 Tile 枚举。那么我应该怎么做呢?

最佳答案

看起来您正在尝试使用名称来表示实例化 Tile[,] 数组的特定方式。

为什么不只声明一个执行此操作的方法?

Tile[,] GetBoard()
{
    return new Tile[8, 8];
}

另一种选择是定义一个 Board 类型,并使用隐式运算符转换为 Tile[, ],如下:

public class Board
{
    private Tile[,] tiles = new Tile[8, 8];

    public static implicit operator Tile[,](Board board)
    {
        return board.tiles;
    }
}

这实际上允许您这样做:

Tile[,] board = new Board();

关于c# - C# 中的 Typedef 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3598498/

相关文章:

mysql,程序中的语法问题

c - C 中的 typedef 和指向函数的指针

c - 在 C 中使用带指针的结构和数组

c# - Rprop 实现

c# - 在 DataGridView 中,在添加新行时将列的 ReadOnly 属性设置为 false,更新其 true (c#.net)

c# - 如何在C#中控制datagridview光标移动

c# - 如何替换 LinkedList 中的元素?

javascript - 将 require() 语法翻译成 import syntax node js

Java\n 行分隔符不起作用

有人可以解释这两个 typedef 之间的区别吗? (函数指针相关)