Any developer
who has worked with C# must have used properties sometimes during development. As
you know, Auto-Property is declared with simple get
and set (i.e. without any backing field),
and can be initialized in the constructor once they are declared.
In C#
6.0 a new features is introduced names as Auto-Property initializer. Auto-Property
initializer allows property to be initialized like any other field in
the same line where it has been declared.
Let’s
see Auto-Property Initializer in action:
public bool UserName { get; set; } = "Sandeep";
|
Auto-Property
initializer in C#
6.0 also allows us to initialize read only Auto-Property in the same line where
it has been declared. In older version of C#, we had to use a private set for
read only properties, but with C# 6.0, without a private set, you can declare and
initialize a property in the same line.
Auto-Property
Initializer for read only properties:
public bool UserName { get; } = "Sandeep";
|
Comments
Post a Comment