相关疑难解决方法(0)

为什么可变结构"邪恶"?

在这里讨论了SO之后,我已经多次读过可变结构是"邪恶"的评论(就像这个问题的答案一样).

C#中可变性和结构的实际问题是什么?

c# struct mutable immutability

470
推荐指数
15
解决办法
7万
查看次数

IEnumerable <object> a = new IEnumerable <object>(); 我可以这样做吗?

我想创建一个对象的新实例 IEnumerable<object>

我可以这样做吗?

IEnumerable<object> a = new IEnumerable<object>();
Run Code Online (Sandbox Code Playgroud)

c#

82
推荐指数
5
解决办法
16万
查看次数

收益率回报== IEnumerable和IEnumerator?

yield return用于实现快捷方式IEnumerableIEnumerator

c# ienumerable ienumerator iterator yield

32
推荐指数
1
解决办法
7862
查看次数

为什么在结构中使用LINQ时必须复制"this"(如果我这样做,那么它是否可以)?

下面的代码在不可变结构中包含一个简单的LINQ查询.

struct Point
{
   static readonly List</*enum*/> NeighborIndexes;
   //and other readonly fields!

    public IEnumerable<FlatRhombPoint> GetEdges()
    {
        return from neighborIndex in NeighborIndexes;
             select GetEdge(neighborIndex);
    }
}
Run Code Online (Sandbox Code Playgroud)

它不编译.

结构体内的匿名方法,lambda表达式和查询表达式无法访问"this"的实例成员.考虑将'this'复制到匿名方法,lambda表达式或查询表达式之外的局部变量,并使用local.

有谁知道为什么不允许这样做?

修复消息建议工作正常:

    public IEnumerable<FlatRhombPoint> GetEdges()
    {
        var thisCopy = this;

        return from neighborIndex in NeighborIndexes;
             select thisCopy.GetEdge(neighborIndex);
    }
Run Code Online (Sandbox Code Playgroud)

但这是标准做法吗?是否有理由在结构中没有这样的查询?(在制作副本的更大方案中,并不担心我的表现如此).

c# linq struct

16
推荐指数
1
解决办法
591
查看次数

标签 统计

c# ×4

struct ×2

ienumerable ×1

ienumerator ×1

immutability ×1

iterator ×1

linq ×1

mutable ×1

yield ×1