Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Do expression bodied properties have backing fields?

Discussion in 'Scripting' started by olidadda, Jan 1, 2021.

  1. olidadda

    olidadda

    Joined:
    Jul 27, 2019
    Posts:
    27
    Hi all, I've seen a lot of people in tutorials write that something like

    upload_2021-1-1_18-19-24.png

    is the same as

    upload_2021-1-1_18-20-40.png

    Now as far as I'm aware, the bottom one isn't an auto-implemented property, so shouldn't it have an explicit backing field? And does that mean the top one should too?

    As far as I'm aware, you need a backing field for a property, and this doesn't generate one.

    I checked this out by doing: upload_2021-1-1_18-30-53.png


    and in the ILDASM I got this:

    upload_2021-1-1_18-31-42.png
     
  2. olidadda

    olidadda

    Joined:
    Jul 27, 2019
    Posts:
    27
    I've just figured out the answer to my own question: Anything with a getter only is readonly and therefore if declared as a property cannot be changed from anywhere else. This includes Expression bodied properties.
     
  3. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,440
    Properties don't _need_ backing fields. Tey can have one, or several if they want. For example this has no backing field since
    degrees
    is a "real" variable intended to be used on its own:

    Code (CSharp):
    1. class A {
    2.   public float degrees;
    3.   public float radians { get { return degrees*DtoR; } set { ... } }
    4. }
    Something like
    string fullName => first+" "+last;
    uses 2 variables, but again, they're already real variables. It has no "backing" variables of its own. This somewhat silly function has 2 backing fields:
    Code (CSharp):
    1. class B {
    2.   int whole;
    3.   float decPart;
    4.  
    5.   public float num {
    6.     get { return num+decPart; }
    7.     set { whole=(int)value; decPart=value-whole; }
    But if we add more functions playing with each part then maybe
    num
    has no backing fields, since it's using someone else's.

    Backing field is really a general term for any private variable. A List is backed by an array (the List class uses a private array to store the list). If you use a property to represent a single virtual variable, which is common in C#, then it often has a backing variable of that exact type, as in:
    Code (CSharp):
    1. class radio {
    2.   int _volume;
    3.   public int volume {
    4.     set { _volume=MathInt.Clamp(volume, 0, 10); }
    5.     get { return _volume; }
    6.   }
    Now, C# auto-properties only create a simple virtual variable. So they always need a single backing variable of the same type they return. That can be created by the compiler. But that's an exception. Usually a human needs to decide what type and how many private variables to use.
     
  4. olidadda

    olidadda

    Joined:
    Jul 27, 2019
    Posts:
    27
    Thank you for your answer.
    I see, but if we did away with the backing fields, isn't that more or less equivalent to making a public variable?
    I suppose maybe not beacuse you can write some extra code in the get or set functions, right?
     
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,440
    What do you mean? Let properties use private variables as much as they want, and make all of the private variables you need. Also don't make any you don't need. If a property is the only thing that uses a certain private variable, you can call it a backing field. if you want.

    The only "official" backing fields are the magic private variables in an auto-property, but auto-properties are super-simple and don't really do anything anyway.
     
    Bunny83 likes this.
unityunity