Properties in C# and use of Properties

Properties are special type of members, declared to read/write private Fields within a declared Type(Class, Struct).
Properties are used as if they are public fields within a declared Type.
Properties have set and get accessor in order to set value to a private fields and get the value of a private fields.
By using properties we can hide private members of our Type.
Properties simplify the code to read or set the data for private fields.
A value keyword is used to define the value assigned by set accessor.

Below is an example of use of property:

+
using System;
 
namespace Properties_in_CSharp
{
 
    public class Student
    {
        string _name;
        float _physics, _chemistry, _biology,_totalMarks,_percentage;
 
        
 
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("Name should not be blank");
                }
 
                _name = value;
 
            }
        }
 
        public float PhysicsMarks
        {
            get
            {
                return _physics;
            }
            set
            {
                if (value <= 0)
                {
                    throw new Exception("Marks in Physics should be greater than zero");
                }
 
                _physics = value;
 
            }
        }
        public float ChemistryMarks
        {
            get
            {
                return _chemistry;
            }
            set
            {
                if (value <= 0)
                {
                    throw new Exception("Marks in Chemistry should be greater than zero");
                }
 
                _chemistry = value;
 
            }
        }
 
        public float BiologyMarks
        {
            get
            {
                return _biology;
            }
            set
            {
                if (value <= 0)
                {
                    throw new Exception("Marks in Biology should be greater than zero");
                }
 
                _biology = value;
 
            }
        }
 
        public float Totalmarks
        {
            get
            {
                return _totalMarks =_physics + _chemistry + _biology;
            }
        }
 
        public float Percentage
        {
            get
            {
                return (_totalMarks / 300) * 100;
            }
        }
    }
 
 
    class UseOfProperties
    {
        static void Main()
        {
            Student stud = new Student();
 
            stud.Name = "Sameer";
            stud.PhysicsMarks = 87;
            stud.ChemistryMarks = 90;
            stud.BiologyMarks = 70;
 
            Console.WriteLine("Name of Student : {0}", stud.Name);
            Console.WriteLine("\nMarks Obtained by {0}",stud.Name);
            Console.WriteLine("Physics : {0}",stud.PhysicsMarks);
            Console.WriteLine("Checmistry : {0}", stud.ChemistryMarks);
            Console.WriteLine("Biology : {0}", stud.BiologyMarks);
            Console.WriteLine("\nTotal Marks obtained : {0}",stud.Totalmarks);
            Console.WriteLine("Percentage Obtained : {0}",stud.Percentage);
 
            Console.ReadLine();
        }
    }
}

In above code sample, We have declared properties to get or set private fields declared with underscore(_) at the top. We can achieve this by creating functions as well, but in that case, we have to separately declare methods for getting and setting values for private fields and also have to call each method to get or set values. By declaring property, We can use same property name to get or set values.
See  value  keyword used in set accessor, Whatever value we assign to our proerty, say e.g. we have stud.Name = "Sameer", the value Sameer gets assigned to value variable in set accessor and from there we can assign that value data to our private fields(In this case _name = value).
).
Also, if you see Totalmarks property, It has only get accessor, that means we can only read the value of private variable.

Below is the output for our above code sample.

Properties in C#

No comments :