1

I have a question that interests me, what would be better in terms of programming: use Class members or Getter from inside the My Class? for example this is my class:

public class myClass
{
    private string _name;
    private int _length;
    private int _weight;

    public void doSomething(myClass obj)
    {

    }

    public void doSomething2(myClass obj)
    {

    }

    public string name
    {
        get { return _name; }
    }

    public int length
    {
        get { return _length; }
    }

    public int weight
    {
        get { return _weight; }
    }
}

using doSomething in this way:

public void doSomething(string str, myClass obj)
{
    string[] arr = str.Split(' ');
    arr[1] = obj._name;
    arr[2] = obj._length;
    arr[3] = obj._weight;
}

or this way:

public void doSomething2(string str, myClass obj)
{
    string[] arr = str.Split(' ');
    arr[1] = obj.name;
    arr[2] = obj.length;
    arr[3] = obj.weight;
}
user1860934
  • 417
  • 2
  • 9
  • 22
  • 4
    Naming convention in C# is to CamelCase property names. So you'd have `public string Name { get { return this.name; } }` – knittl Sep 20 '13 at 06:44
  • 1
    Also, methods start with upper case.But to answer your question, I always use the Getters, just to be consistent. – Loetn Sep 20 '13 at 06:45
  • 1
    Either make those methods static or don't provide MyClass object as a parameter. – S_F Sep 20 '13 at 06:47
  • And in case i want to return MyClass object: using return this ? – user1860934 Sep 20 '13 at 06:54
  • In your case, I'd get rid of the private backing fields completely and just use auto properties like `public Name { get; private set; }`. Normal properties could have (a little) additional logic, for example checking if the instance is now "changed" or not. The decision between using a backing field or the property in that scenario depends on whether or not you want that addidional logic to be executed. For the "changed" example you probably do, for other examples you might not. – Corak Sep 20 '13 at 06:54

2 Answers2

1

If you provide a Getter for a variable, I would use the getters. Just to be consistent. If you use both of them at the same time, it can get confusing (for you and for fellow programmers).

If you use the getters everywhere - and in the future perform a code-search for that property - you will find all of them, but if you sometimes use the private variable, you will miss some.

And this answer: Should you access your private variables through properties inside your class?

Community
  • 1
  • 1
Loetn
  • 3,832
  • 25
  • 41
0

Answer by Oded

  • When using Auto-Implemented properties, you don't have a choice - you must use the property, as you don't have any access to the generated field.

  • If you property is not simple and does some extra work (validation, firing events etc...), you should call the property in order to centralize access and logic.

  • If you have any other properties (meaning a simple property with no logic and a backing field) I would ask why are they not one of the above...

  • With the example you have give, it makes little difference - it is more important to be consistent with how you use these and really boils down to personal aesthetics and coding style.

Answer by TheCodeJunkie

  • One of the stronger argument for accessing local (class scope) variables through properties is that you add a level of abstraction in your class. If you change any logic concerning how that field is stored then the rest of your code will be left unaffected.

  • For example you might change that from a local variable to a property of a child object, to a database call, to a webservice call, to a static property on a class and so on. When making the change it gives you a single point of change, the property, and you do not have to update the rest of your class since they all use the property.

  • Also using the property enables you to apply business rules on the value of the property instead of having to enforce the same rule at each location where you'd directly access the field. Again, encapsulation

  • With the introduction of automatic properties there's even less reason to explicitly have a local variable, unless you need to apply business rules on the get/set

Community
  • 1
  • 1
Kurubaran
  • 8,696
  • 5
  • 43
  • 65