Tuesday, 7 November 2017

Auto Property Initializer in C# 6.0

In this article, We will discuss about How to Use Auto Property Initializer in C# 6.0.

Auto Property is a new concept in C# 6.0 to set the value of a property during of property declaration itself. So, if we want to set some of the default values to a property then in the declaration itself we can do that. In our Previous versions, we were declared the values in Constructor. So now, there is no need of any Constructor and any extra line of code.
For Example:
namespace ConsoleApplication3
{
    class Program
    {
        public int MyProperty { getset; } = 25;

        static void Main(string[] args)
        {
            Program p = new Program();
            int s = p.MyProperty;

            p.MyProperty = 78;
            int s1 = p.MyProperty;
        }
    }
}

In the Class program I created one Property called MyProperty and assigning 25 as it’s default value.

When I bind the MyProperty to the variable s then it will display 25 and in the next line I am assigning 78 so whenever the next time I will call MyProperty then it will Show 78.

0 comments:

Post a Comment