Khalil Liraqui
Khalil Liraqui

Reputation: 415

How fields are created from properties in C# .net

When you create a property does it automatically create field in the same class ? is it possible to define its the protection level for example every time you create a property it will create a protected field ?

Upvotes: 0

Views: 51

Answers (1)

Fabio
Fabio

Reputation: 32455

Auto-implemented properties is exactly what you trying to accomplish
From MSDN

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

protected int MyProperty { get; set; }

In C#6 you can declare readonly auto-implemented properties

protected int MyProperty { get; }

Upvotes: 1

Related Questions