static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Nullable Data *****\n");
            DatabaseReader dr = new DatabaseReader();

            //  Get int from "database".
            int? i = dr.GetIntFromDatabase();
            if (i.HasValue)
            {
                Console.WriteLine("Value of i is {0}", i.Value);
            }
            else
                Console.WriteLine("Value of 'i' is undefined");

            //  Get bool from "database".
            bool? b = dr.GetBoolFromDatabase();
            if (b !=null)
            {
                Console.WriteLine("Value of 'b' is: {0}", b.Value);
            }
            else
                Console.WriteLine("Value of 'b' is undefined");

            // If the value from GetIntFromDatabase() is null,
            // assisgn local variable to 100
            int myData = dr.GetIntFromDatabase() ?? 100;
            Console.WriteLine("Value of myData: {0}", myData);

            Console.ReadKey();
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with nullable data *****\n");
            DatabaseReader dr = new DatabaseReader();
            //get int from "database"
            int? i = dr.GetIntFromDatabase();
            if (i.HasValue)
                Console.WriteLine("Value of 'i' is: {0}", i.Value);
            else
                Console.WriteLine("Value of 'i' is undefined.");
            //get bool from "Database"
            bool? b = dr.GetBoolFromDatabase();
            if (b != null)
                Console.WriteLine("Value of 'b' is: {0}", b.Value);
            else
                Console.WriteLine("Value of 'b' is undefined.");

            int myData = dr.GetIntFromDatabase() ?? 100;//could also be done long-
            //hand via an if/else statement.
            Console.WriteLine("Value of myData: {0}", myData);

            TesterMethod(null);

            Console.ReadLine();
        }
Example #3
0
        static void Main(string[] args)
        {
            // Uncomment to generate compiler errors.
            // bool myBool = null;
            // int myInt = null;

            Console.WriteLine("***** Fun with Nullable Data *****\n");
            DatabaseReader dr = new DatabaseReader();

            // Get int from 'database'.
            int?i = dr.GetIntFromDatabase();

            if (i.HasValue)
            {
                Console.WriteLine("Value of 'i' is: {0}", i);
            }
            else
            {
                Console.WriteLine("Value of 'i' is undefined.");
            }

            // Get bool from 'database'.
            bool?b = dr.GetBoolFromDatabase();

            if (b != null)
            {
                Console.WriteLine("Value of 'b' is: {0}", b);
            }
            else
            {
                Console.WriteLine("Value of 'b' is undefined.");
            }

            // If the value from GetIntFromDatabase() is null,
            // assign local variable to 100.
            int?myData = dr.GetIntFromDatabase() ?? 100;

            Console.WriteLine("Value of myData: {0}", myData);
            Console.ReadLine();
        }
Example #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with nullable data *****\n");
            DatabaseReader dr = new DatabaseReader();

            int? i = dr.GetIntFromDatabase();

            if(i.HasValue)
                Console.WriteLine("Value of i is: {0}", i.Value);
            else
                Console.WriteLine("Value of i is undefined.");

            bool? b = dr.GetBoolFromDatabase();
            if(b != null)
                Console.WriteLine("Value of b is: {0}", b.Value);
            else
                Console.WriteLine("Value of b is undefined.");

            int myData = dr.GetIntFromDatabase() ?? 100;
            Console.WriteLine("Value of myData is: {0}", myData);

            Console.ReadLine();
        }
Example #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Nullable Data *****\n");
            DatabaseReader dr = new DatabaseReader();

            //从"数据库"获取int
            int?i = dr.GetIntFromDatabase();

            if (i.HasValue)
            {
                Console.WriteLine("Value of 'i' is:{0}", i.Value);
            }
            else
            {
                Console.WriteLine("Value of 'i' is undefined");
            }

            //从"数据库"获取bool
            bool?b = dr.GetBoolFromDatabase();

            if (b != null)
            {
                Console.WriteLine("Value of 'b' is :{0}", b.Value);
            }
            else
            {
                Console.WriteLine("Value of 'b' is undefined");
            }
            Console.WriteLine();

            //从GetIntFromDatabase()返回的值为null时,将局部变量赋值为100
            int myData = dr.GetIntFromDatabase() ?? 100;

            Console.WriteLine("Value of myData:{0}", myData);

            Console.ReadLine();
        }
Example #6
0
        static void Main(string[] args)
        {
            //bool myBool = null; ERROR: non-nullable value type

            string myString = null; //OK, Strings are reference types

            LacalNullableVariables();

            DatabaseReader dr = new DatabaseReader();
            int? i = dr.GetIntFromDatabase();
            if (i.HasValue)
            {
                Console.WriteLine("Value of i is: {0}", i.Value);
            }
            else
            {
                Console.WriteLine("Value of i is undefined");
            }

            bool? b = dr.GetBoolFromDatabase();
            if (b == null) {
                Console.WriteLine("b in undefined");
            }
            else
            {
                Console.WriteLine("Value of b is: {0}", b.Value);
            }

            int myData = dr.GetIntFromDatabase() ?? 100;
            Console.WriteLine("Value of myData: {0}", myData);
            
            
            Console.ReadLine();


        }
Example #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Nullable Data *****\n");

            DatabaseReader dr = new DatabaseReader();

            int?i = dr.GetIntFromDatabase();

            if (i.HasValue)
            {
                Console.WriteLine("Value of 'i' is {0}", i.Value);
            }
            else
            {
                Console.WriteLine("Value of 'i' is undefined.");
            }

            bool?b = dr.GetBoolFromDatabase();

            if (b != null)
            {
                Console.WriteLine("Value of 'b' is {0}", b.Value);
            }
            else
            {
                Console.WriteLine("Value of 'b' is undefined.");
            }

            // If the value from GetIntFromDatabase() is null,
            // assign local variable to 100.
            int myData = dr.GetIntFromDatabase() ?? 100;

            Console.WriteLine("Value of myData: {0}", myData);

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            // Compiler errors!
            // Value types cannot be set to null!
            //bool myBool = null;
            //int myInt = null;

            // OK! Strings are reference types.
            string myString = null;

            Console.WriteLine("***** Fun with Nullable Data *****\n");
            DatabaseReader dr = new DatabaseReader();

            // Get int from "database".
            int?i = dr.GetIntFromDatabase();

            if (i.HasValue)
            {
                Console.WriteLine("Value of 'i' is: {0}", i.Value);
            }
            else
            {
                Console.WriteLine("Value of 'i' is undefined.");
            }
            // Get bool from "database".
            bool?b = dr.GetBoolFromDatabase();

            if (b != null)
            {
                Console.WriteLine("Value of 'b' is: {0}", b.Value);
            }
            else
            {
                Console.WriteLine("Value of 'b' is undefined.");
            }

            // If the value from GetIntFromDatabase() is null,
            // assign local variable to 100.
            int myData = dr.GetIntFromDatabase() ?? 100;

            Console.WriteLine("Value of myData: {0}", myData);

            // Long-hand notation not using ?? syntax.
            int?moreData = dr.GetIntFromDatabase();

            if (!moreData.HasValue)
            {
                moreData = 100;
            }
            Console.WriteLine("Value of moreData: {0}", moreData);

            Console.WriteLine($"I'm passing null:\n");
            TesterMethod(null);

            Console.WriteLine($"I'm passing null:\n");
            TesterMethodByUsingNullConditionalOperator(null);

            Console.WriteLine($"I'm passing null:\n");
            TesterMethodByUsingNullConditionalAndNullCoalescingOperator(null);

            Console.ReadLine();
        }