Wednesday, August 17, 2011

Properities in C#

Property in C# in a method to access data members..it has get and set accesors.These are basically used to keep the data private.

Program 1)To calculate grade and percentage using properties.

class marks
{
private int phy;
private int che;
private int mth;
private char ch;

public int physics
{
set
{
phy=value;
}
get
{
return(phy);
}
}
public int chemistry
{
set
{
che=value;
}
get
{
return(che);
}
}
public int math
{
set
{
mth=value;
}
get
{
return(mth);
}
}
public int total
{
get
{
int total=phy+che+mth;
return(total);
}
}
public float percentage
{
get
{
float percent=(total/3);
return(percent);
}
}
public char grade
{
get
{
int p=(total/3);
if(p>70)
{
ch='A';
}
else
{
ch='B';
}
return(ch);
}
}
}

class abc
{
static void Main()
{
marks p1 = new marks();
p1.chemistry=87;
p1.math=79;
p1.physics=90;

System.Console.WriteLine("Total is {0}",p1.total);
System.Console.WriteLine("Percentage is {0} %",p1.percentage);
System.Console.WriteLine("Grade is {0}",p1.grade);

}
}

No comments:

Post a Comment