Skip to content

Instantly share code, notes, and snippets.

@atodorova atodorova/Person.cs Secret
Created Mar 21, 2013

Embed
What would you like to do?
using System;
using System.Text;
class Person
{
public string Name { get; set; }
public int? Age { get; set; }
public Person(string name) : this(name, null)
{
}
public Person(string name, int? age)
{
if (name == null || name == string.Empty)
{
throw new Exception("Name must be specified!");
}
this.Name = name;
this.Age = age;
}
public override string ToString()
{
StringBuilder person = new StringBuilder();
person.AppendFormat("Person Name: {0}", this.Name).AppendLine();
person.AppendFormat(this.Age == null ? "The person has no age!" : "Person Age:" + this.Age ).AppendLine();
return person.ToString();
}
}
using System;
class PersonInformation
{
static void Main()
{
Person[] persons =
{
new Person("Pesho", 45),
new Person("Tosho"),
new Person("Sasho", 16 ),
new Person("Asia"),
};
foreach (var person in persons)
{
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("Person Information:");
Console.WriteLine("------------------");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(person);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.