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?

Jwosty
  • 3,497
  • 2
  • 22
  • 50
  • I know I throw a SIGSEGV every time I see someone use `42` in an example. Its not funny anymore, folks. Go back to using `69`, it needs to get the dust knocked off it. –  Aug 08 '12 at 16:24
  • 1
    It should rather give some kind of stack overflow since you're calling the property recursively until the stack explodes... And what?? Mono bombs you with SIGSEGV errors instead of runtime exceptions? – Paul Michalik Aug 08 '12 at 17:02
  • It's probably for a similar reason as [this](http://stackoverflow.com/questions/10807059/segmentation-fault-11) poster's question... Both mine and his should be stack overflow exceptions, but both are some kind of segfaults. Strange, yes. – Jwosty Aug 08 '12 at 17:11

2 Answers2

3

Try changing your code to this:

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

OR this:

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

You don't have a backing store. You either need to add one, or use an auto property. The way your code is written you are recursively calling get/set when you are accessing those properties.

pstrjds
  • 16,840
  • 6
  • 52
  • 61
1

You can use auto-implemented properties:

public int Bar { get; set; }

or you can work with field:

private int _bar;
public int Bar 
{ 
    get { return value; } 
    set { _bar = value; } 
}
Anton
  • 127
  • 1
  • 3
  • 11