CSharp - Create and use an auto property

Introduction

Consider the following code:

//private int myInt;
public int MyInt{
    get;set;
}

This declaration is called an automatic property declaration.

The compiler will input the intended code for us in this case to make our lives easier.

Demo

using System;

class MyClass/*ww w  . j  av a 2s. c  o m*/
{
    public int MyInt
    {
        get; set;
    }
}
class Program
{
    static void Main(string[] args)
    {
        MyClass ob = new MyClass();
        ob.MyInt = 10;
        Console.WriteLine("\nValue of myInt is now:{0}", ob.MyInt);
        ob.MyInt = 100;
        Console.WriteLine("Now myInt value is:{0}", ob.MyInt);
    }
}

Result

Related Topic