what should i do so that value of variable "age" will become 0,if entered value of "age"<0 , by making changes only in constructor's body??
Class Person{
public int age;
public Person(int initial_Age)
{
if (initial_Age < 0)
{
Console.WriteLine("This person is not valid, setting age to 0.");
}
}
static void Main(string[] args)
{
Console.WriteLine("enter age");
int age = int.Parse(Console.ReadLine());
Person p= new Person(age);
Console.WriteLine("Age={0}",age);
Console.ReadKey();
}
}

Ranjit PowarRank #553 · 4.4K reputationPosted Jan 23, 2016, 4:36 PM EST
{
public int age;
public Person(int initial_Age)
{
if (initial_Age < 0)
{
age=0;
}
}
static void Main(string[] args)
{
Console.WriteLine("enter age");
int age = int.Parse(Console.ReadLine());
Person p= new Person(age);
Console.WriteLine("Age={0}",p.age);
Console.ReadKey();
}
}
If it helps you select it as correct answer.
ravindra agnihotri8 reputationPosted Jan 23, 2016, 8:11 PM EST