Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // new features in C# 6
            // null propagating operator
            // string interpolation
            // auto property initialisation
            // nameof operator
            Console.WriteLine($"Propertyname: {Person.FirstNamePropertyName}");

            var persons = new List<Person>();
            var p1 = new Person();
            persons.Add(p1);
            var p2 = new Person
            {
                FirstName = $"{p1.FirstName} Sepp"
            };
            persons.Add(p2);
            Person p3 = null;
            persons.Add(p3);
            
            // runtime exception filter with when
            foreach (var p in persons)
            {
                //var firstname = p?.FirstName;
                //if (firstname == null)
                //    firstname = "null";
                //Console.WriteLine($"{firstname} {p?.LastName}");
                try {
                    Console.WriteLine($"{p.FirstName} {p.LastName}");
                } catch(NullReferenceException) when (p == null)
                {
                    Console.WriteLine("person is null");
                }
            }

            // new dictionary initializer
            //var dict = new Dictionary<int, Person>
            //{
            //    [1] = p1
            //};

            // expresson bodied members
            Console.WriteLine($"Fullname: {p1.GetFullName()}");

            Console.ReadKey();

        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Person A = new Person();

            A.FirstName  = "Jesse";
            A.LastName   = "Carter";
            A.Age        = 29;
            A.Occupation = "IT";

            Person B = new Person();

            B.FirstName  = "Megan";
            B.LastName   = "Eliser";
            B.Age        = 23;
            B.Occupation = "Manager";

            Car C = new Car();

            C.Year  = 2005;
            C.Make  = "Mini";
            C.Model = "Cooper";
            C.Color = "Blue";

            Car D = new Car();

            D.Year  = 2014;
            D.Make  = "Chevy";
            D.Model = "Cobalt";
            D.Color = "Black";

            A.Transporter = C;
            B.Transporter = D;

            Console.WriteLine(A.GetFullName());
            Console.WriteLine(B.GetFullName());
            Console.ReadLine();
        }