-3

So this is one of my first classes for Object-Oriented Development and my feedback for my code was that I was missing "a property" and that GetName() is a method rather than a property. I'm currently coding in c# and was wondering if anyone could explain what a property is or point me in the right direction I'd be very appreciative.

        //Instance Variables
    private String name;
    private decimal balance;
    //Variables
    public AccountDetails(String name, decimal balance)
    {
        this.name = name;
        this.balance = balance;
    }
    //Accessor Methods
    public String GetName()
    {
        return this.name;
    }
  • 6
    You need to read about [properties](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties). Getter methods, as in your example, are more often used in Java than C#. – Johnathan Barclay Jul 26 '21 at 09:54
  • The property that replaces `GetName` would be like this in C#: `public string Name { get { return this.name; } }` or `public string Name => this.name;` – Martin Jul 26 '21 at 09:55
  • For future reference: typing "c# docs X" into your favourite search engine (e.g. "c# docs property") should yield a documentation result fairly high in the search results. – ProgrammingLlama Jul 26 '21 at 09:56
  • 2
    @Martin For simple field access like this, an auto-implemented property should be used i.e. `public string Name { get; }` – Johnathan Barclay Jul 26 '21 at 09:57
  • @JohnathanBarclay I'm sorry but that's the wrong assumption to make without understanding why `name` has been implemented as a member variable. I notice that OP did not receive feedback from their tutor about that – Martin Jul 26 '21 at 09:58
  • @Martin The property is simply returning the field value, so manually implementing the property adds verbosity with no benefit whatsoever. If `Name` needs to be reassigned in other methods, then it can be changed to `public string Name { get; private set; }`. – Johnathan Barclay Jul 26 '21 at 10:03
  • @JohnathanBarclay You've got no idea what else is going on in this class. OP has specifically laid out the class to use member variables and therefore I commented a solution that used them. Just because it _could_ also be done using an auto-implemented property doesn't mean it _should_ be done like that. You don't have enough information to make that determination – Martin Jul 26 '21 at 10:09

2 Answers2

1

You could have replaced your method with property like

public string Name { get { return this.name; } }

There is basically no difference between methods and properties. Getters and setters get internally translated into standard methods such that the runtime has no idea whether some getter or setter is associated with a certain property.

However, there is an important software engineering benefit: your code tends to be easier to understand if you restrict yourself to use getters and setters with get and set semantics. I.e. do only the steps necessary to provide the respective property.

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties

Amit Kotha
  • 1,641
  • 1
  • 11
  • 16
  • And of course, C# syntax allows the property accessors (setters and getters) to be invoked so it looks like access to a field (type-level variable), for example instead of `set_Prop(newValue)` you use `Prop = newValue`. Even though the latter is just a fancy syntax for the former (on the IL level), syntax makes a difference. – Jeppe Stig Nielsen Jul 26 '21 at 10:39
-1

GeeName() is a method, could be called Getter in Java. Properties in C# work a bit differently, the declaration goes like

//no direct variable declaration
public String Name1 { get; set; }

//with variable declaration and custom getter logic
//notice, that properties don't need to implement both GET and SET (but they can)
private String _name;
private int _access_counter = 0;
public String Name2 
{
    get 
    {
        _access_counter++;
        return _name + _access_counter;
    }
}

public String Name3
{
    set 
    {
        _access_counter--;
        _name = value;
    }
}
Klemikaze
  • 369
  • 6
  • 15