Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            // 1. Auto-property initializers 
            var p = new Person();
            p.Title = "Test";
            //p.Age = 5; // Error, no setter
            //p.Age is 10

            // 2. Index initializers
            p.Stats = new Dictionary<string, string>()
            {
                ["Bob"] = "10 points",
                ["Steve"] = "12 points"
            };

            // 3. String interpolation
            Console.WriteLine($"{p.Id} and some string: {p.Age}");

            // 4. Expression bodied functions & properties
            var ageFromProperty = p.DoubleAgeProperty;
            var ageFromMethod = p.DoubleAgeMethod(10);

            // 5. Null conditional operator
            var name = p.FullName?.Length; // no exception, gets null
            name = p.FullName?.Length ?? 5;

            // Events
            p.OnChange += P_OnChange;
            p.DoChange(); 

            // 6. nameOf expression
            p.DoSomething(null);
        }
        Person search(string title, string alias, Person source)
        {
            if (source.title == title) {
                return source.alias == alias ? source : null;
            }

            Person result;
            foreach(Person sub_source in source.subordinate)
            {
                result = search(title, alias, sub_source);
                if (result != null)
                {
                    return result;
                }
            }

            return null;
        }