Introduction
Property In C#
A property is an entity that describes the features of an object. A property is
a piece of data contained within a class that has an exposed interface for
reading/writing.
Main purpose
Properties are used to access our private class members and used them like a
variables.
Properties are special kind of method or block which have two successors and
this property cant take any arguments.
Properties are look like a variables.
Properties are used to security access our private class members.
Properties increase usability with maintaining security.
The two successor of Properties are "get" and "set"
get -- used to return the value.
set -- used to assign the value.
Syntax of Property
public
returnType PropertyName
{
set { privateVariable = value;}
get { return
privateVariable; }
}
Types of Properties
1) Read-Only (only get)
2) Write-Only(only set)
3) Normal (get & set both)
Program Read-Only
using
System;
namespace
property
{
class
ReadOnlyProp
{
string name="DEEPAK
DWIJ "; // private variable-bcoz ByDefault
member variable of class is Private.
public
string ROnly // Read only property only
returns the private variable.
{
get {
return name; }
}
}
class Program:ReadOnlyProp
{
public static
void Main()
{
Program rdonly =
new Program();
Console.WriteLine(rdonly.ROnly);
Console.Read();
}
}
}
Output


Join the conversation! Your thoughts help the community grow.