0% found this document useful (0 votes)
37 views12 pages

Properties

Uploaded by

f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views12 pages

Properties

Uploaded by

f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Create and Using Properties

• Before going on properties lets have a look at


why the concept of properties came into C#?
The is because of two reasons:

– If the members of a class are private then how


another class in C# will be able to read, write, or
compute the value that field.

– If the members of the class are public then


another class may misuse that member.
Example
using System; public class Demo1
public class stud {
{ public static void Main()
// public data members {
stud obj = new stud();
public int rn;
obj.rn = 101;
public string name;
obj.name = "Heer";
// private field // setting values to private
// private int marks = 35; //obj.marks = 0;
} Console.WriteLine("Name:{0}
\n Roll No:{1}",obj.name,obj.rn);

}
}
• Explanation: In above you can see that public members of
class stud can be accessed by class Demo1 and using the
object "obj" of stud it can provide the values to the
members rn and name .

• Demo1 cannot provide the value to the member "marks"


because it is private in stud .

• To test the private member access remove the comments


and try to run and you can see compiler will give an error.

Using Properties
What is Property ?
• Properties are the special type of class members
that provides a flexible mechanism to read,
write, or compute the value of a private field.

• Properties can be used as if they are public data


members, but they are actually special methods
called accessors.

• Encapsulation and hiding of information can also


be achieved using properties.
• It uses pre-define methods which are “get” and
“set” methods which helps to access and modify
the properties.

• Accessors: The block of “set” and “get” is known


as “Accessors”.

• Use get accessor statements to provide read


accessor
• Use set accessor statements to provide write
accessor
• Restrictions of Properties :
– As property dose note have a strong location ,it
can’t be passed as ref and out parameter to a
method.
– Property cannot be overloaded.
• Restrictions of Accessors :
– Accessors do not have parameters.
– A property does not need to have both a get and a
set accessor. For example, a read only property
will provide only get accessor.
• There are different types of properties based on
the “get” and set accessors:

• Read and Write Properties: When property


contains both get and set methods.

• Read-Only Properties: When property contains


only get method.

• Write Only Properties: When property contains


only set method.
• The syntax for Defining Properties:
<access_modifier> <return_type> <property_name>
{
get { return <attribute name>; }
set { <attribute name> = value; }
}

• Example
public string Name
{
get{return name;}
set{name =value;}
}
• A get property accessor is used to return the
property value, and a set property accessor is
used to assign a new value.

• The value keyword is used to define the value


being assigned by the set accessor.

• You can also declare set property with private


modifier so the only member of class can access
it like below:
– Public string Name{get; private set;}
Example of read/write
using System; class TestStudent
public class Student {
{ public static void Main()
// Declare name field {
private string names; Student s = new Student();
s.Name = "Pooja";
// Declare name property Console.WriteLine("Name:
public string Name " + s.Name);
{ }
get{return name;} }
set{name = value;} • Output
} – Name : Pooja
}
Example of read only
using System;
public class Student
{
private string name= "Pooja Patel";
public string Name
{
get{return name;}
}
}
class TestStudent
{
public static void Main()
{
Student s = new Student();
Console.WriteLine("Name: " + s.Name);
}
}
Output :- Name : Pooja Patel

You might also like