using System;

class Name : IFormattable
{
    public string firstname;
    public string secondname;

    public Name(string firstname, string secondname)
    {
        this.firstname = firstname;
        this.secondname = secondname;
    }

    public override string ToString() { return ToString(null, null); }

    public string ToString(String format)
    {
        return ToString(format, null);
    }

    public string ToString(IFormatProvider fp)
    {
        return ToString(null, fp);
    }

    public string ToString(string format, IFormatProvider fp)
    {
        // kein Format
        if (format == null)
            return String.Format("{0} {1}", firstname, secondname);

        // "g"-Format
        if (format == "g" || format == "G")
            return String.Format("{0} {1}", firstname, secondname);

        // "i"-Format, erst Familienname, dann Vorname)
        if (format == "i" || format == "I")
            return String.Format("{0}, {1}", secondname, firstname);

        // "s"-Format, nur Nachname
        if (format == "s" || format == "S")
            return secondname;

        // Nicht unterstützte Formate
        throw new FormatException(String.Format("Ungültiger Formatbezeichner: '{0}'.", format));
    }
}


class IFormattableDemo
{
    static void Main()
    {
        Name n = new Name("Leopold", "Bloom");

        string s = n.ToString("I");
        Console.WriteLine(" " + s);

        Console.WriteLine(" {0}", n);
        Console.WriteLine(" {0:G}", n);
        Console.WriteLine(" {0:I}", n);
        Console.WriteLine(" {0:S}", n);
    }
}
