1

I am perplexed as in my spare time I've been reading through C# books to become familiar with the language.

I stumbled upon the use of properties; in this context it is in regards to using a getter/setter for a privately declared field in my class.

This is what I have in my code as to keep it simple:

class ClassName
{
   private int hWorked = 24;
   public int HoursWorked
     {
       get
        {
          return hWorked;
        }
     }
}

Now the book says that:

If I use the short hand version - public int HoursWorked {get;} - that it is the same as the above code.

But what I need clarification on is how the shorthand is able to return the hWorked value without specifying that the hWorked = value.

In simple terms: How does the HoursWorked getter know to target my privately declared hWorked.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 3
    `public int HoursWorked {get;} ` creates its own backing field and *doesn't address* `hWorked` – Dmitry Bychenko Sep 29 '17 at 08:53
  • I think the book means that if you replace *both* the variable and the property with just an auto-implemented property the (observable) result is the same – UnholySheep Sep 29 '17 at 08:55
  • Now with regards to that, if I were to use the shorthand to create an anonymous backing field would I remove the private variable hWorked as it is not needed in this particular instance. Also is there a way to find the name of this backing field? – Gary Stewart Sep 29 '17 at 08:57
  • that is correct – praty Sep 29 '17 at 08:58
  • 1
    @GaryStewart The backing field isn't necessarily named anything obvious or helpful. It can be accessed via reflection as an exercise, but as a rule, if you need a backing variable within scope, you need to create it yourself in order to maintain control of it. – Alex Sep 29 '17 at 09:06
  • What you call *shorthand* is *auto-implemented property*. And such property has backfield, see [this question](https://stackoverflow.com/q/8817070/1997232). – Sinatr Sep 29 '17 at 09:07
  • What Alex said, but you can in fact restrict setter access: `public int HoursWorked{ get; private set;}` and use `HoursWorked = 24` inside the class. – Fildor Sep 29 '17 at 09:08

2 Answers2

3

Well public int HoursWorked {get;} creates its own backing field and doesn't address hWorked. The equivalent of the code in the question (shorthand version) is

  class ClassName {
    public int HoursWorked { get; } = 24;
  }

You can see the backing field with a help of Reflection:

  using System.Reflection;

  ... 

  var fields = string.Join(", ", typeof(ClassName)
    .GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
    .Select(f => f.Name));

  Console.Write(fields);

Outcome (may vary):

  <HoursWorked>k__BackingField

If you inspect the initial ClassName implementation you'll get

  hWorked
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

the shorthand version uses a "hidden" variable to store the values in.

if you write public int hWorked {get; set;} it reads and writes from a unnamed variable in the background.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Vulpex
  • 1,041
  • 8
  • 19