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.
Step 2- Now Double Click on ENTER Button and write the following code which is given below:-
Description- Here i have used two Namespace.
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 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.
Step-2 Now Double click on Enter Button and Write the code which is given below:-
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:-
I Hope this is helpful for you.
To Get the Latest Free Updates Subscribe
Click below for download whole application.
Download
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 .
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.
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;
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
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-3 Now Run (press F5).Step-3 Now Run (press F5).
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); } } }
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:-
- Call by Value and Call by Reference
- Structure and Class
- E-Post System Project
- Create a Setup File
- ASP and ASP.NET
- Ajax in ASP.NET
To Get the Latest Free Updates Subscribe
Click below for download whole application.
Download
hie there thanx for wonderful tutorial . just want to know why we use this get set methods does it have any alternative..??