The backing fields are automatically private - am I right?
class Car
{
public String Mark { get; set; }
public String Model { get; set; }
}
The backing fields are automatically private - am I right?
class Car
{
public String Mark { get; set; }
public String Model { get; set; }
}
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;
}
}
In
class Car
{
public string Mark { get; set; }
string Model { get; set; }
}
Car
is internal.Mark
is public.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).