Jitesh Byahut 7/2/2014 2711

Properties in C#

Property is used to access the private variable of a class from outside of the class. Properties are member of class that provide flexible mechanism to read, write or compute the values of private fields. It enable a class to expose a public way of getting and setting values.

Property is always public.

Property can never accept arguments.

Property used two accessor, get and set.

get property accessor is used to return the property value.

set property accessor is used to assign a new value.

Property provides a way to transfer data from public member to private member.

Write only property

public string Name

{

   set { _name = value; }

}

Read only property

public string Name

{

   get { return _name; }

}

Read write property

public string Name

{

   get { return _name; }

   set { _name = value; }

}

Properties provide encapsulation concept

Encapsulation allow to hiding data and implementation. Suppose we have Student class and it's have a private string variable _name, we want access that variable but not directly access, then on that condition we use Properties.

           public class Student

    {

        private string _name;

       

       

        public string Name

        {

            get { return _name; }

            set { value = _name; }

        }

 

        public void Show()

        {

            Console.WriteLine("Your Name"+" "+_name);

        }

    }

 

 

    class Program

    {

        static void Main(string[] args)

        {

            Student student = new Student();

                  // Here we cannot access private variable of class directly

            // which is hiding internal details that is encapsulation 

            student.Name = "Jitesh";

            student.Show();

 

            Console.ReadKey();

        }

    }

Property provide Validations

Property provides validation. Suppose there is class Movie that task is to collect movie ratting. Many viewers enter their favourite movie name and ratting in term of star that rang is 0-5 but not more than 5 stars.

Imagine a condition where a viewer enter movie name null and ratting 6 stars, then this application will be waste. So you can implement validation that validate if movie name is null and also if ratting star in more than 5 then it automatically set to 5.

    public class MovieRatting

    {

        private string _movie;

        private int _star;

 

        public int Star

        {  

            get { return _star; }

            set

            {

                // Max value is 5

                if (value <= 5)

                {

                    _star = value;

                }

                else

                {

                    // if value is more than 5 then it will auto set value as 5

                    _star = 5;             

                }

            }

        }

        public string Movie

        {

            get

            {

                return _movie;

            }

            set

            {

                if (value == null)

                {

                    throw new Exception("Movie name cannot null");

                }

                _movie = value;

            }

        }

 

        public void Show()

        {

            Console.WriteLine("Movie Name" + " - " + _movie);

            Console.WriteLine("Ratting" + " - " + _star);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                

                MovieRatting movie = new MovieRatting();

                // Movie name cannot be null

                movie.Movie = null;

                // Movie's star cannot be more than 5

                movie.Star = 6;

                movie.Show();

            }

            catch (Exception Ex)

            {

                Console.WriteLine(Ex.Message);

            }

 

            Console.Read();

        }

    }


Please give your feedback for improving this page