- Course
- OOP
- Constructors and destructors
Constructors and destructors
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that prompts the user for three names of people and stores them in an array of Person-type objects. To do this, first create a Person class that has a Name property of type string
, a constructor that receives the name as a parameter, a destructor that assigns the name to empty and overwrites the ToString () method. p>
End the program by reading the people and executing the ToString () method on screen.
Input
Juan
Sara
Carlos
Output
Hello! My name is Juan
Hello! My name is Sara
Hello! My name is Carlos
Solution
using System;
public class ConstructorsAndDestructors
{
public static void Main(string[] args)
{
int total = 3;
Person[] persons = new Person[total];
for (int i = 0; i < total; i++)
{
persons[i] = new Person(Console.ReadLine());
}
for (int i = 0; i < total; i++)
{
Console.WriteLine(persons[i].ToString());
}
}
public class Person
{
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
public override string ToString()
{
return "Hello! My name is " + Name;
}
~Person()
{
Name = string.Empty;
}
}
}