4

In my class I have private variable, which I use inside the class only through get/set. Sometimes I forget, that I shouldn't use variable directly (even within the class) and must use get/set.

How to make that the only way to use a variable were get/set?

public class A {
    int x;

    public XVariable {
        get { return x; }
        set { x = value }

        // some additional operations
    }

    void SomeMethod() {
        x = 5; // no
        XVariable = 5; // yes
    }
}
Amazing User
  • 3,473
  • 10
  • 36
  • 75

3 Answers3

10

C# has auto properties. No backing field needed in your code.

public class A {
    public XVariable {
        get;
        set;
     }
}

You can also have different access modifiers. Like if you want to only be able to set it from within the class.

public class A {
    public XVariable {
        get;
        private set;
     }
}

There won't be a backing field accessible from your code, but the compiler will generate one in the MSIL (what C# compiles to). You don't have to worry about that part though.

A potential downside Joe pointed out to auto props, sometimes you need to perform other actions (especially event handlers) in your property when you set something. But that's not possible with auto props. In that case, his answer would be more appropriate. But if that's not a concern for your use case, then my answer should be sufficient.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
  • Technically that still *generates* a backing field, but at least you can't set it in code :) – BradleyDotNET Feb 13 '15 at 20:51
  • @BradleyDotNET If you can't touch it, it don't exist! But yes, point taken, I'll update the answer with the explanation. – mason Feb 13 '15 at 20:52
  • 2
    A potential reason someone may want to use a field and a regular property would be so there can be more "stuff" in the getter and setter, like event handlers or other things with side effects - which would be why they don't want to access the field directly, but instead always access the property. – Joe Enos Feb 13 '15 at 20:53
  • This is a much better answer, since it directly addresses the question. You could alternatively use the other version (where you define the backing field yourself) when you need to do anything more advanced than simple field visibility. – patricknelson Feb 02 '18 at 00:49
5

You can create a base class, and do all your real work in the derived class:

public class SomeBaseClass {
    private int _x;
    public int X { get { return _x; } set { _x = value; } }
}

public class DerivedClass : SomeBaseClass {
    void DoSomething() {
        // Does not have access to _x
    }
}
Joe Enos
  • 39,478
  • 11
  • 80
  • 136
0

Many people prefix their private variables with an underscore to help signify the variable is private. (Although it is a matter of opinion, some people like it and some don't) There is a bit more insight on this question.

You can however, scrap the field and use an auto property such as:

public XVariable { get; set; }

An auto property will store an anonymous backing field "out of view".

Community
  • 1
  • 1
Cyral
  • 13,999
  • 6
  • 50
  • 90