Properties In C#

By
Hi Friends ! Here i am going to tell you what is Properties in c#. You can download this whole application from bottom after reading the Tutorials.
Properties  can be defined to Set and Get the values of private variables of a class.By using properties we can help proper hold over the variable uses.
Meaning of Set and Get access or:-
Set -> Write only not Read .
Get ->Read only not Write .
Suppose if you declare Set access-or  only then you can write only value.
Suppose if you declare Get access-or  only then you can Read only value.

Now I am going to implement the Properties in C# using Windows Forms Application . Please follow the following steps which are given below.

Step 1- Open  your visual studio -> File -> New -> Project -> Select Windows Forms Application -> click OK ->Make Form like given below.


Step 2-  Now Double Click on ENTER Button and write the following code which is given below:-


using System;
using System.Windows.Forms;

namespace properties
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class STUDENT
        {
            String name;
            
            public string PropName1
            {
                set
                {
                    name = value;

                }

                get
                {
                    return name;
                }
            }
            int age;
            public string PropName2
            {
                set
                {
                    age = Convert.ToInt32(value);
                }

                get
                {
                    return age.ToString();
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            STUDENT obj1 = new STUDENT();
            STUDENT obj2 = new STUDENT();
            
            obj1.PropName1 = textBox1.Text;
            obj2.PropName2 = textBox2.Text;
            MessageBox.Show(obj1.PropName1, obj2.PropName2);

            //  MessageBox.Show(obj2.PropName2);      
        }
    }
}
 
Step 3- Now Run (press F5) the application and Enter the value in  Required Field.You will see following output which is given below:-





Description-  Here i have used two Namespace.
  • using System;
  • using System.Windows.Forms;
if you know  more about namespace then remove one by one namespace and Read the Error . you will find why  did i use this namespace.

Here  i have created a student class and declared the name ,age variables as a Private. After  that i have  created Propname1 and Propname2   Properties.Under this i have defined Set and Get access-or.
After that i have created the  two object of student class.
  • obj1
  • obj2
Here i have created two object of student class  to  differentiated the properties.if you want to create only one object you can create it that is working well.

Note:-  Here i have declared name and age variable as a private so that other class could not use this variable instead of the student class. 
if we declare the variable name as a public then we can access these variables from other class member so here there is no use of C# Properties.

In C# Version 3.0 if we does not give the body of Set and Get access or then compiler automatic create the body of  Set and Get access or .This feature is called automatic property feature of c# (3.0).C# version 2.0 does not support this feature it gives compile time error.

Now again i am going to implement the automatic property feature of C#.which are given below:-

Step-1  Again open your visual studio and follow the previous step which is mentioned above.
see it:


Step-2 Now Double click on Enter Button and Write the code which is given below:-

using System;
using System.Windows.Forms;

namespace automatic_properties
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class STUDENT
        {
           // String name;  No need for use

            public string PropName
            {
                set;
                get;

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           STUDENT obj = new STUDENT();
            
            obj.PropName = textBox1.Text;
            MessageBox.Show(obj.PropName);

        }
    }
    }


Step-3  Now Run (press F5).
see output:




Note:- In case of automatic property feature we can not make property read only or write only.But in C# properties i can do it.
For More:-
  1. Call by Value and Call by Reference
  2. Structure and Class
  3. E-Post System Project
  4. Create a Setup File
  5. ASP and ASP.NET
  6. Ajax in ASP.NET
I Hope this is helpful for you.
To Get the Latest  Free Updates Subscribe
Click below for download whole application.
             Download

1 comment:

  1. hie there thanx for wonderful tutorial . just want to know why we use this get set methods does it have any alternative..??

    ReplyDelete

Powered by Blogger.