Перегрузить метод ToString для представления вектора - C#
Формулировка задачи:
Перегрузить метод ToString для представления вектора, заключенного в скобки
(любой формы), в виде строки для класса Vector.
Решение задачи: «Перегрузить метод ToString для представления вектора»
textual
Листинг программы
class Vector
{
int year;
int population;
public Vector(int _y, int _p)
{
year = _y;
population = _p;
}
public override string ToString()
{
return string.Format("{0,6} {1,15:N0}", year, population);
}
}
static void Main(string[] args)
{
int[] years = { 2013, 2014, 2015 };
int[] population = { 1025632, 1105967, 1148203 };
Console.WriteLine(string.Format("{0,6} {1,15}\n", "Year", "Population"));
for (int index = 0; index < years.Length; index++)
Console.WriteLine(new Vector(years[index], population[index]).ToString());
Console.ReadKey();
}