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) .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.