1

The backing fields are automatically private - am I right?

class Car
{
    public String Mark { get; set; }
    public String Model { get; set; }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Heron
  • 329
  • 2
  • 5
  • 15

2 Answers2

3

Auto-implemented properties:

public String Mark { get; set; }
public String Model { get; set; }

When you declare a auto-implemented as shown in your example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

private string mark;
private string model;
public String Mark { 
   get
   {
   return mark;
   }
   set
   {
   mark = value;
   }
}
public String Model{ 
   get
   {
   return model;
   }
   set
   {
   model = value;
   }
}
Llazar
  • 3,167
  • 3
  • 17
  • 24
0

In

class Car
{
    public string Mark { get; set; }
    string Model { get; set; }
}
  • The class Car is internal.
  • The property Mark is public.
  • The property Model is private.

The comments seem to indicate that you are asking about the accessibility of the backing fields.

Both properties are Auto-Implemented Properties (C# Programming Guide) having a hidden, non-accessible backing field. The documentation for C# auto-implemented properties says:

When you declare a property [...], the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

This is different in VB where the backing field is accessible from within the class: Auto-Implemented Properties (Visual Basic).

Also see: What are the default access modifiers in C#?

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188