2
Answers

change in variable using constructor

Ask a question

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();
}
}

Answers (2)