小编Eri*_*ers的帖子

为什么Point.Offset()没有在readonly结构中给出编译器错误?

也许我误解了readonly结构的概念,但我认为这段代码不应该编译:

public readonly struct TwoPoints
{
    private readonly Point one;
    private readonly Point two;

    void Foo()
    {
        // compiler error:  Error CS1648  Members of readonly field 'TwoPoints.one'
        // cannot be modified (except in a constructor or a variable initializer)
        one.X = 5;

        //no compiler error! (and one is not changed)
        one.Offset(5, 5);
    }
 }
Run Code Online (Sandbox Code Playgroud)

(我正在使用C#7.3.)我错过了什么?

.net c# struct readonly-attribute

4
推荐指数
1
解决办法
125
查看次数

标签 统计

.net ×1

c# ×1

readonly-attribute ×1

struct ×1