-1

I have a question regarding fields and properties. I am stuck regarding one matter and that is when it comes to Constructors. We took how the convention of naming Properties is with title-casing. So if the field is ‘age’ then the Property will be ‘Age’. Then when it came to the Constructors section of CodeAcademys Learning C#, we took that when creating a Constructor you Initialize the Field before then you set it in the body of the Constructor. This is the example code in the lesson:

class Forest
{
    public int Area;

    public Forest(int area)
    {
        Area = area;
    }
}

Here is where my problem comes…

If we are saying that the Field is going to be lowercased then why does it say in the lesson, “We can add code in the constructor to set values to fields:” How are we setting a field if the naming convention is of that which indicates it to be Property? Also where is the Field for the Area property?

I hope someone can help me. Thanks!

I tried to see find the difference between the fields and properties in Constructors but haven't seemed to have found a clear answer.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
hj2001
  • 1
  • 1
  • 4
    You don't have a property in your class, only a field. – Klaus Gütter Feb 07 '23 at 13:54
  • 1
    `public int Area {get; set;}` to declare `Area` as a *property*, not *field* – Dmitry Bychenko Feb 07 '23 at 13:54
  • If you want to keep the field lowercase, i.e. `area` instead of `Area` you can put it as `this.area = area;` we mark with `this` to let compiler know we mean field or property, not argument or local variable – Dmitry Bychenko Feb 07 '23 at 13:58
  • This is the part where I am getting confused... – hj2001 Feb 07 '23 at 14:06
  • If I put `area` is this not the convention for setting fields? And if you do `Area` then this is indicating towards the property – hj2001 Feb 07 '23 at 14:07
  • https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property – Johnathan Barclay Feb 07 '23 at 14:10
  • I think it would be rather rare for you to have both a field and property with the same name. Having `Area` start with a capital letter indicates that it is public rather than being a field or property. If it were private it would start with an underscore: `_area`. for local/scoped variables and parameters people tend to use lowercase without an underscore – Roe Feb 07 '23 at 14:10
  • Hmmm thats very strange. As the course I am taking on CodeAcademy states this for naming properties: The `Area` property is associated with the `area` field. It’s common to name a property with the title-cased version of its field’s name, e.g. `age` and `Age`, `name` and `Name`. – hj2001 Feb 07 '23 at 14:12
  • Or am I misunderstanding what was said? – hj2001 Feb 07 '23 at 14:13
  • what is meant is that usually you will use a default getter and setter `Public string Area {get; set;)`, this property creates fields for itself. Only when writing custom getters and setters wil you really need a field combined with a property – Roe Feb 07 '23 at 14:21

1 Answers1

0

public int Area; is a field, not a property. You would declare a property like this:

private int _area; // Backing field of the property.
public int Area // Property (characterized by the get and set keywords).
{
    get { // Property getter.
        return _area;
    }
    set { // Property setter.
        _area = value;
    }
}

It is common to prepend fields (variables declared in the class or struct) with an underscore to distinguish them from parameters and local variables (variables declared in a method).

You can initialize the backing field (the field where the property stores its data) like this:

public Forest(int area)
{
    _area = area;
}

If you do not perform any additional logic in the property, you can also use an auto-property which does all the above automatically:

public int Area { get; set; }

You can initialize the property like this:

public Forest(int area)
{
    Area = area;
}

If by accident you have a field with the same name as the constructor parameter, you can write the field as this.fieldName:

public Forest(int area)
{
    this.area = area;
}

You can think of a property as a set of methods used to access a field. The idea is to let an object manage its own data and to forbid direct access to the data from outside. In the property setter, you could add code to validate the data or to sanitize the data. E.g., you could trim leading and trailing spaces from a string value.

Often getter-only properties are used to combine other properties:

public string property FirstName { get; set; }
public string property LastName { get; set; }
public string property FullName { get { return FirstName + " " + LastName; } }

See also my answer to the question How can I find a specific element in a List? where I explain C# properties in detail.

As @KlausGütter explains in a comment, it is the syntax which distinguishes properties from fields. This can be confusing for someone learning C#. C# has no keywords for fields, properties and methods. You have to look carefully at the syntax. This is different e.g., in Visual Basic, where you have keywords like Dim (variable/field declaration), Property, Sub and Function.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • So in this case would 'private int _area;' be the field and then 'public Area' would be the property? – hj2001 Feb 07 '23 at 14:05
  • 1
    Yes your right. So if I do `private int _area` this is the field. And `public int Area` would be the property? – hj2001 Feb 07 '23 at 14:09
  • 3
    @hj2001 No: whether or not it is a property is not determined by identifier casing (this is just convention), but by the presence of `get` and/or `set` – Klaus Gütter Feb 07 '23 at 14:21