0

When I try to implement a get-set-property with a body and use that set, it always exits with a SIGSEGV -- a segmentation fault. I'm running Mono 2.10.9 + MonoDevelop 3.0.3.5 all under Mac OS X Mountain Lion (10.8). Here's the minimum amount of code I can get it to do this with:

public class MainClass {
    public static int Main(string[] args) {
        Foo foo = new Foo();
        foo.Bar = 42;        // Never makes it past this line
        return 0;
    }
}

public class Foo {
    public int Bar {
        get { return Bar; }
        set { Bar = value; }
    }
}

Am I doing something wrong, or is this a Mono bug?

4

2 に答える 2

3

コードを次のように変更してみてください。

public class Foo {
    public int Bar { get; set; }
}

またはこれ:

public class Foo {
    private int _bar;
    public int Bar {
       get { return _bar; }
       set { _bar = value; }
    }
}

バッキング ストアがありません。追加するか、自動プロパティを使用する必要があります。コードの記述方法では、これらのプロパティにアクセスするときに get/set を再帰的に呼び出しています。

于 2012-08-08T16:24:53.767 に答える
1

自動実装されたプロパティを使用できます。

public int Bar { get; set; }

または、フィールドで作業できます:

private int _bar;
public int Bar 
{ 
    get { return value; } 
    set { _bar = value; } 
}
于 2015-10-29T10:43:54.743 に答える