Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get Set property in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 175
    Comment on it

    Normally we can declare a datafield and assign value to that datafield, so why do we use GET and SET accessor to return or assign the value of datafield? Let us go through an example to understand GET and SET. A class Employee is having the private field of EmpName,Designation,Salary hence we can not use these fields outside the class but by using GET and SET,we can access the values of these private fields through properties. Get is called read only property and Set is called write only property so together we use them for reading and writing the values.

     

    Get: Get property is used to return the value of data field.

    Syntax:

    get
    {
    return _datafield;
    }

     

    Set: Set Property is used to set the value of datafield.

    Syntax:

    set
    {
    _datafield = value;
    }

    Include the following code in your console application to understand the working of GET and SET. Here we are taking a Company_name datafield and we are accessing the properties in Example class.

    For Example:

    Using System;
    
    namespace ConsoleApplicationExm
    {
    
        class Company
        {
            string Comapny_name;
            public string Company_Name
            {
                get { return Comapny_name; }
                set { Comapny_name = value; }
            }
        }
        class Example
        {
            static void Main(string[] args)
            {
    
                Company obj = new Company();
                 obj.Company_Name = ("My company name is" + " ") + "EvonTechnology"; // calling set { }
                 Console.WriteLine(obj.Company_Name); // calling get { }
                 Console.ReadLine();
            }
        }
    }
    

    Output: We are getting output "EvonTechnology" as Set property is used to assign the value and get is used to return the same value.Please refer the screenshot.

    To understand it in a better way, let us take one more example of windows application.

    In this example, we will use two forms : FormSource and FormDestination.We will add two textboxes and two buttons in FormSource and two textboxes and one button in FormDestination.

    Functionality: We will get the values in two textboxes of FormSource and on click of show button we will set these values in the textboxes of FormDestination. So In FormDestination,We are accessing the values through properties of GetSetClass in which we are taking two datafields name and companyname.

    Include the following code on button click of FormSource and FormDestinationand refer to the below screenshot:

    FormSource.cs

    private void Show_Click(object sender, EventArgs e)
            {
                if (textBox1.Text == "" || textBox2.Text == "")
                {
                    MessageBox.Show("Please don't leave any textbox empty");
                }
                else
                {
                    GetSetClass .Name = textBox1.Text;
                    GetSetClass .Company_Name = textBox2.Text;
                    FormDestination ob = new FormDestination();
                    ob.Show();
                }
            }
    
            private void Close_Click(object sender, EventArgs e)
            {
                this.Close();
            }

     

    FormDestination.cs

    private void Close_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void FormDestination_Load(object sender, EventArgs e)
            {
                textBox1.Text = GetSetClass.Name;
                textBox2.Text = GetSetClass.Company_Name;
            }

     

    GetSetClass.cs

    static string name, Companyname;
            public static string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
            public static string Company_Name
            {
                get
                {
                    return Companyname;
                }
                set
                {
                    Companyname = value;
                }
            }

     

    See the Output:

    First we are inserting the values in textboxes of FormSource, when we click on show button:it will display the value in textboxes of FormDestination. Refer the screenshot given below:

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: