Home » C# auto-initialize property

C# auto-initialize property

by Online Tutorials Library

C# auto-initialize property

C# auto-initialize property is a feature, introduced in 6.0. It allows us to initialize properties without creating a constructor.

Now, we can initialize properties along with declaration.

In early versions, constructor is required to initialize properties. An old approach is used in the following example.

C# Example without auto-initialize property

Output

Rahul Kumar 

Now, we don’t need to create constructor. See, the following example.

C# Example with auto-initialize property

Output

Rahul Kumar 

We can make a property restrict to change by setting private modifier. See, the following example.

C# Example with auto-initialize property

Output

Rahul Kumar 

You may also like