Exemplo n.º 1
0
        /// <summary>
        /// Casting
        /// </summary>
        private void workWithCasting( )
        {
            Trace.WriteLineIf(ts.TraceInfo, "Trace: Call workWithCasting()");

            Runner r1 = new Runner("John", new DateTime(1994, 4, 18), 15, 80);

            if (r1 is APerson)
            {
                APerson p1 = r1;
                tbTopLeft.Text += $"\n{p1.StringToWrite( )}\n";
            }

            APerson p2 = new APerson();

            p2.Name        = "Kate";
            p2.DateOfBirth = new DateTime(1985, 11, 23);

            Runner r2 = p2 as Runner;

            if (r2 != null)
            {
                tbTopLeft.Text += $"\n{r2.Name} was born at {r2.DateOfBirth}\n";
            }
            else
            {
                tbTopLeft.Text += "p2 couldn't be cast to Runner type";
            }
        }