Esempio n. 1
0
        // since 8.0
        public void StaticFunctions()
        {
            var instance = new UsefulObject();

            instance.Name = "Matthew";
            instance.Age  = 26;

            var result = Calculate(instance);
Esempio n. 2
0
            static int Calculate(UsefulObject inst)
            {
                if (inst.Name == "Matthew")
                {
                    return(inst.Age);
                }

                return(-1);
            }
Esempio n. 3
0
        public void Demonstrate(UsefulObject instance)
        {
            // get it or throw
            int justGetIt = instance.Name !.Length;

            // get it or null
            int?tryGetIt = instance.Name?.Length;

            // try get it or use 0
            int fixIt = instance.Name?.Length ?? 0;

            if (instance.Name == null)
            {
                return;
            }

            // at this point, Name can't be null
            int magic = instance.Name.Length;
        }
Esempio n. 4
0
        // since 7.0
        public void LocalFunctions()
        {
            var instance = new UsefulObject();

            instance.Name = "Matthew";
            instance.Age  = 26;

            var result = Calculate();

            int Calculate()
            {
                // Oh dear, we captured obj
                if (instance.Name == "Matthew")
                {
                    return(instance.Age);
                }

                return(-1);
            }
        }