In C#, properties are the natural extension of data
fields. They are also known as ‘smart field’.
Inside a class, we declare a data field as a private
and provide a set of public set and get method to access the data fields,
because the data field cannot be accessed directly outside the class so we use
get and set methods.
Example:
using System;
using
System.Collections.Generic;
using System.Text;
using
System.Windows.Forms;
namespace
WindowsFormsApplication38
{
public class
Class1
{
private
int i;
public int prop
{
get
{
return
i;
}
set
{
i = value;
}
}
}
}
.cs
code:
private void
button1_Click(object sender, EventArgs e)
{
Class1
obj = new Class1();
int
x = obj.prop;
x = 10;
MessageBox.Show(x.ToString());
}
Output
:
No comments:
Post a Comment