Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the { get; set; } syntax in C#?

Tags:

c#

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; }?

like image 241
kn3l Avatar asked Feb 23 '11 20:02

kn3l


People also ask

What is get set in C?

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.

What is get set used for?

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.

What does get set mean?

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.

What is a get or set accessor?

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.


1 Answers

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;     } } 
like image 126
Klaus Byskov Pedersen Avatar answered Oct 03 '22 15:10

Klaus Byskov Pedersen