class Person { privatestring m_name = "N/A"; private int m_Age = 0; //Declare a Name property of type string: publicstring Name { get { return m_name; } set { m_name = value; } } // Declare an Age property of type int: publicint Age { get { return m_Age; } set { m_Age = value; } } publicoverride string ToString() { return "Name = " + Name + ", Age = " + Age; } } class TestPerson { staticvoid Main() { //Create a new Person object: Person person = new Person(); //Print out the name and the age associated with the person: System.Console.WriteLine("Person details - {0}", person); //Set some values on the person object: person.Name = "Joe"; person.Age = 99; System.Console.WriteLine("Person details - {0}", person); //Increment the Age property: person.Age += 1; System.Console.WriteLine("Person details - {0}", person); } } Person details - Name = N/A, Age = 0 Person details - Name = Joe, Age = 99 Person details - Name = Joe, Age = 100
|