I am learning ASP.NET MVC and I can read English documents, but I don't really understand what is happening in this code:
public class Genre { public string Name { get; set; } }
What does this mean: { get; set; }
?
Get set are access modifiers to property. Get reads the property field. Set sets the property value. Get is like Read-only access. Set is like Write-only access.
The get method returns the value of the variable name . The set method assigns a value to the name variable. The value keyword represents the value we assign to the property. If you don't fully understand it, take a look at the example below.
Prepare to go, as in Get set; the taxi's coming. This phrase is also a synonym for get ready. Also see under all set.
In properties, a get accessor is used to return a property value and a set accessor is used to assign a new value. The value keyword in set accessor is used to define a value that is going to be assigned by the set accessor. In c#, the properties are categorized as read-write, read-only, or write-only.
It's a so-called auto property, and is essentially a shorthand for the following (similar code will be generated by the compiler):
private string name; public string Name { get { return this.name; } set { this.name = value; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With