gpt4 book ai didi

c# - 返回对列表元素的引用以便能够为其赋值

转载 作者:行者123 更新时间:2023-11-30 14:06:06 26 4
gpt4 key购买 nike

我来自 C++ 背景,这里有一个简单的问题。作为类(class)成员,我有以下列表:

public enum FieldColor {Empty, Blue, White};
private List<List<FieldColor>> m_board = new List<List<FieldColor>>();

现在我有了这么简单的方法:

private FieldColor GetFieldColor(GridCoordinates coordinates) {
return m_board[coordinates.row][coordinates.column];
}

现在我想用这个方法给一个coordinate赋值,我写了这个:

    GetFieldColor(coordinates) = color;

我得到这个错误:

The left-hand side of an assignment must be a variable, property or indexer [Assembly-CSharp]

有什么解决办法。

最佳答案

你总是可以使用 Ref return

Starting with C# 7.0, C# supports reference return values (ref returns). A reference return value allows a method to return a reference to a variable, rather than a value, back to a caller. The caller can then choose to treat the returned variable as if it were returned by value or by reference. The caller can create a new variable that is itself a reference to the returned value, called a ref local.

限制

There are some restrictions on the expression that a method can return as a reference return value. Restrictions include:

  • The return value must have a lifetime that extends beyond the execution of the method. In other words, it cannot be a local variable in the method that returns it. It can be an instance or static field of a class, or it can be an argument passed to the method. Attempting to return a local variable generates compiler error CS8168, "Cannot return local 'obj' by reference because it is not a ref local."

  • The return value cannot be the literal null. Returning null generates compiler error CS8156, "An expression cannot be used in this context because it may not be returned by reference."

  • A method with a ref return can return an alias to a variable whose value is currently the null (uninstantiated) value or a nullable type for a value type.

  • The return value cannot be a constant, an enumeration member, the by-value return value from a property, or a method of a class or struct. Violating this rule generates compiler error CS8156, "An expression cannot be used in this context because it may not be returned by reference."

示例

private ref int GetFieldColor(GridCoordinates coordinates)
{
return ref m_board[coordinates.row][coordinates.column];
}

有效用法

GetFieldColor(gridCoordinates) = 345;

或者您可以使用 indexer

Indexers allow instances of a class or struct to be indexed just like arrays. The indexed value can be set or retrieved without explicitly specifying a type or instance member. Indexers resemble properties except that their accessors take parameters.

public int this[GridCoordinates coordinates]
{
get => m_board[coordinates.row][coordinates.column];
set => m_board[coordinates.row][coordinates.column] = value;
}

用法

this[GridCoordinates] = 345

关于c# - 返回对列表元素的引用以便能够为其赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50678902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com