Sunday, December 28, 2014

Program to show the use of properties

9:16 PM - By azeheruddin khan 0

Explanation: - Properties are members of a class which are used to access and manipulate private instance variables of a class. In the above given program, the property: countries consists of get and set accessors,  ‘get’ is used for accessing the  private variable ‘name’ and ‘set’ is used to manipulate the value of the variable ‘name’.


 The static property: capitals can only access and manipulate static variables like ‘capital’.

Example:


using System;

namespace PropertiesExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Country c = new Country();
            Console.WriteLine("COUNTRY:" + c.countries);
            Console.WriteLine("after changing name");
            c.countries = "INDIA";
            Console.WriteLine("COUNTRY:" + c.countries);
            Console.WriteLine();
            Country o = new Country();
            Console.WriteLine("CAPITAL:" + Country.capitals);
            Console.WriteLine("after changing name");
            Country.capitals = "NEW DELHI";
            Console.WriteLine("CAPITAL:" + Country.capitals);
            Console.ReadLine();

        }
    }

    class Country
    {
        private string name = "CHINA";
        private static string capital = "BEIJING";
        public string countries
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public static string capitals
        {
            get
            {
                return capital;
            }
            set
            {
                capital = value;
            }
        }
    }

}


Output:




About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top