1

I'm learning C#, and the whole properties thing with get; and set; is weirding me out... So many unnecessary mistakes could be made by accidentally using = instead of ==...

Comments
  • 0
    Also intrigued
  • 3
    = assign
    == compare values

    It’s the same in most languages.
  • 6
    What did you before c# if this is writing you out, that's a common concept in many programming languages
  • 0
    @C0D4 I know.. I come from java where you have to create getters and setters manually. Having access to something inside an object of a different class that I can use just = to change makes me uneasy.
  • 0
    @g-m-f I come from java where you have to create getters and setters manually. Having access to something inside an object of a different class that I can use just = to change makes me uneasy
  • 0
    private string _foo;
    public string Foo
    { get { return _foo;}
    set { _foo = value;}
    }
  • 0
    um... what
    arent those
    like
    encapsulated fields
    what the
    i thought they were automatically set to private if i did that

    WHAT THE FUCK
  • 0
    @bkwilliams Seeing as you're not manipulating the values at any point why not just do
    public string Foo { get; set; }
  • 0
    @itsdaniel0 I would but writing code on a phone is painful. Depending on the case I leave off the set so callers have only the == option.
  • 0
    @bkwilliams public string Foo { get; private set; } 😉
  • 1
    <code>
    private string _thingy;
    public string getThingy()
    {
    return _thingy;
    }

    public void setThingy(string value)
    {
    _thingy = value;
    }
    </code>

    vs.

    <code>
    public string Thingy { get; set; }
    </code>

    I think it's well worth risking a mistake with = and == which is usually rare and easy to catch and fix
  • 0
    This is also where the Yoda statements prove useful (not a fan myself)

    if("foo" == Foo) {}
    vs
    if(Foo == "foo") {}

    That way it'll throw an error since you can't assign to a string
  • 0
    Umm, just use any decent IDE or editor. AFAIK, = doesn't return a Boolean so it should error out.
Add Comment