Esempio n. 1
0
        //</snippet14>

        static void Main(string[] args)
        {
            //<snippet1>
            var currentPerformanceCounterCategory = new System.Diagnostics.
                                                    PerformanceCounterCategory();
            //</snippet1>


            int val1 = 1;
            int val2 = 2;
            int val3 = 3;

            //<snippet2>
            if ((val1 > val2) && (val1 > val3))
            {
                // Take appropriate action.
            }
            //</snippet2>

            //<snippet3>
            // The following declaration creates a query. It does not run
            // the query.
            //</snippet3>


            // Save snippet 4 and 5 for possible additions in program structure.

            Name[] nameList = { new Name {
                                    FirstName = "Anderson", LastName = "Redmond"
                                },
                                new Name {
                                    FirstName = "Jones", LastName = "Seattle"
                                },
                                new Name {
                                    FirstName = "Anderson", LastName = "Redmond"
                                } };
            int    n = 0;

            //<snippet6>
            string displayName = $"{nameList[n].LastName}, {nameList[n].FirstName}";

            //</snippet6>

            Console.WriteLine("{0}, {1}", nameList[n].LastName, nameList[n].FirstName);
            Console.WriteLine(nameList[n].LastName + ", " + nameList[n].FirstName);

            //<snippet7>
            var phrase      = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
            var manyPhrases = new StringBuilder();

            for (var i = 0; i < 10000; i++)
            {
                manyPhrases.Append(phrase);
            }
            //Console.WriteLine("tra" + manyPhrases);
            //</snippet7>

            //<snippet8>
            // When the type of a variable is clear from the context, use var
            // in the declaration.
            var var1 = "This is clearly a string.";
            var var2 = 27;
            var var3 = Convert.ToInt32(Console.ReadLine());
            //</snippet8>

            //<snippet9>
            // When the type of a variable is not clear from the context, use an
            // explicit type.
            int var4 = ExampleClass.ResultSoFar();
            //</snippet9>

            //<snippet10>
            // Naming the following variable inputInt is misleading.
            // It is a string.
            var inputInt = Console.ReadLine();

            Console.WriteLine(inputInt);
            //</snippet10>

            //<snippet11>
            var syllable = "ha";
            var laugh    = "";

            for (var i = 0; i < 10; i++)
            {
                laugh += syllable;
                Console.WriteLine(laugh);
            }
            //</snippet11>

            //<snippet12>
            foreach (var ch in laugh)
            {
                if (ch == 'h')
                {
                    Console.Write("H");
                }
                else
                {
                    Console.Write(ch);
                }
            }
            Console.WriteLine();
            //</snippet12>

            //<snippet13>
            // Preferred syntax. Note that you cannot use var here instead of string[].
            string[] vowels1 = { "a", "e", "i", "o", "u" };


            // If you use explicit instantiation, you can use var.
            var vowels2 = new string[] { "a", "e", "i", "o", "u" };

            // If you specify an array size, you must initialize the elements one at a time.
            var vowels3 = new string[5];

            vowels3[0] = "a";
            vowels3[1] = "e";
            // And so on.
            //</snippet13>


            //<snippet15>
            // In the Main method, create an instance of Del.

            // Preferred: Create an instance of Del by using condensed syntax.
            Del exampleDel2 = DelMethod;

            // The following declaration uses the full syntax.
            Del exampleDel1 = new Del(DelMethod);

            //</snippet15>

            exampleDel1("Hey");
            exampleDel2(" hey");

            // #16 is below Main.
            Console.WriteLine(GetValueFromArray(vowels1, 1));

            // 17 requires System.Drawing
            //<snippet17>
            // This try-finally statement only calls Dispose in the finally block.
            Font font1 = new Font("Arial", 10.0f);

            try
            {
                byte charset = font1.GdiCharSet;
            }
            finally
            {
                if (font1 != null)
                {
                    ((IDisposable)font1).Dispose();
                }
            }


            // You can do the same thing with a using statement.
            using (Font font2 = new Font("Arial", 10.0f))
            {
                byte charset = font2.GdiCharSet;
            }
            //</snippet17>


            //<snippet18>
            Console.Write("Enter a dividend: ");
            var dividend = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter a divisor: ");
            var divisor = Convert.ToInt32(Console.ReadLine());

            // If the divisor is 0, the second clause in the following condition
            // causes a run-time error. The && operator short circuits when the
            // first expression is false. That is, it does not evaluate the
            // second expression. The & operator evaluates both, and causes
            // a run-time error when divisor is 0.
            if ((divisor != 0) && (dividend / divisor > 0))
            {
                Console.WriteLine("Quotient: {0}", dividend / divisor);
            }
            else
            {
                Console.WriteLine("Attempted division by 0 ends up here.");
            }
            //</snippet18>

            //<snippet19>
            var instance1 = new ExampleClass();
            //</snippet19>

            //<snippet20>
            ExampleClass instance2 = new ExampleClass();
            //</snippet20>

            //<snippet21>
            // Object initializer.
            var instance3 = new ExampleClass {
                Name     = "Desktop", ID = 37414,
                Location = "Redmond", Age = 2.3
            };

            // Default constructor and assignment statements.
            var instance4 = new ExampleClass();

            instance4.Name     = "Desktop";
            instance4.ID       = 37414;
            instance4.Location = "Redmond";
            instance4.Age      = 2.3;
            //</snippet21>

            // #22 and #23 are in Coding_Conventions_WF, below.

            // Save 24 in case we add an exxample to Static Members.

            ExampleClass.totalInstances = 1;


            var customers = new List <Customer>
            {
                new Customer {
                    Name = "Jones", ID = 432, City = "Redmond"
                }
            };

            // Check shop name to use this.
            var distributors = new List <Distributor>
            {
                new Distributor {
                    Name = "ShopSmart", ID = 11302, City = "Redmond"
                }
            };

            //<snippet25>
            //<snippet28>
            var seattleCustomers = from customer in customers
                                   //</snippet28>
                                   where customer.City == "Seattle"
                                   select customer.Name;
            //</snippet25>

            //<snippet26>
            var localDistributors =
                from customer in customers
                join distributor in distributors on customer.City equals distributor.City
                select new { Customer = customer, Distributor = distributor };
            //</snippet26>

            //<snippet27>
            var localDistributors2 =
                from customer in customers
                join distributor in distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };
            //</snippet27>

            //<snippet29>
            var seattleCustomers2 = from customer in customers
                                    where customer.City == "Seattle"
                                    orderby customer.Name
                                    select customer;
            //</snippet29>

            // #30 is in class CompoundFrom


            var customerDistributorNames =
                from customer in customers
                join distributor in distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            var customerDistributorNames2 =
                from customer in customers
                from distributor in distributors
                where customer.City == distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            foreach (var c in customerDistributorNames)
            {
                Console.WriteLine(c);
            }

            // Could use in Static Members.
            Console.WriteLine(BaseClass.IncrementTotal());
            // Do not do access the static member of the base class through
            // a derived class.
            Console.WriteLine(DerivedClass.IncrementTotal());

            //// Error
            //var instance = new ExampleClass();
            //Console.WriteLine(instance.IncrementTotal());
        }
Esempio n. 2
0
        //</snippet14b>

        static void Main(string[] args)
        {
            //<snippet1>
            var currentPerformanceCounterCategory = new System.Diagnostics.
                                                    PerformanceCounterCategory();
            //</snippet1>

            int val1 = 1;
            int val2 = 2;
            int val3 = 3;

            //<snippet2>
            if ((val1 > val2) && (val1 > val3))
            {
                // Take appropriate action.
            }
            //</snippet2>

            //<snippet3>
            // The following declaration creates a query. It does not run
            // the query.
            //</snippet3>

            // Save snippet 4 and 5 for possible additions in program structure.

            Name[] nameList = { new Name {
                                    FirstName = "Anderson", LastName = "Redmond"
                                },
                                new Name {
                                    FirstName = "Jones", LastName = "Seattle"
                                },
                                new Name {
                                    FirstName = "Anderson", LastName = "Redmond"
                                } };
            int    n = 0;

            //<snippet6>
            string displayName = $"{nameList[n].LastName}, {nameList[n].FirstName}";

            //</snippet6>

            Console.WriteLine("{0}, {1}", nameList[n].LastName, nameList[n].FirstName);
            Console.WriteLine(nameList[n].LastName + ", " + nameList[n].FirstName);

            //<snippet7>
            var phrase      = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
            var manyPhrases = new StringBuilder();

            for (var i = 0; i < 10000; i++)
            {
                manyPhrases.Append(phrase);
            }
            //Console.WriteLine("tra" + manyPhrases);
            //</snippet7>

            //<snippet8>
            var var1 = "This is clearly a string.";
            var var2 = 27;
            //</snippet8>

            //<snippet9>
            int var3 = Convert.ToInt32(Console.ReadLine());
            int var4 = ExampleClass.ResultSoFar();
            //</snippet9>

            //<snippet10>
            var inputInt = Console.ReadLine();

            Console.WriteLine(inputInt);
            //</snippet10>

            //<snippet11>
            var syllable = "ha";
            var laugh    = "";

            for (var i = 0; i < 10; i++)
            {
                laugh += syllable;
                Console.WriteLine(laugh);
            }
            //</snippet11>

            //<snippet12>
            foreach (char ch in laugh)
            {
                if (ch == 'h')
                {
                    Console.Write("H");
                }
                else
                {
                    Console.Write(ch);
                }
            }
            Console.WriteLine();
            //</snippet12>

            //<snippet13a>
            string[] vowels1 = { "a", "e", "i", "o", "u" };
            //</snippet13a>
            //<snippet13b>
            var vowels2 = new string[] { "a", "e", "i", "o", "u" };
            //</snippet13b>
            //<snippet13c>
            var vowels3 = new string[5];

            vowels3[0] = "a";
            vowels3[1] = "e";
            // And so on.
            //</snippet13c>


            //<snippet15a>
            ActionExample1("string for x");

            ActionExample2("string for x", "string for y");

            Console.WriteLine($"The value is {FuncExample1("1")}");

            Console.WriteLine($"The sum is {FuncExample2(1, 2)}");
            //</snippet15a>
            //<snippet15b>
            Del exampleDel2 = DelMethod;

            exampleDel2("Hey");
            //</snippet15b>
            //<snippet15c>
            Del exampleDel1 = new Del(DelMethod);

            exampleDel1("Hey");
            //</snippet15c>


            // #16 is below Main.
            Console.WriteLine(GetValueFromArray(vowels1, 1));

            // 17 requires System.Drawing
            //<snippet17a>
            Font font1 = new Font("Arial", 10.0f);

            try
            {
                byte charset = font1.GdiCharSet;
            }
            finally
            {
                if (font1 != null)
                {
                    ((IDisposable)font1).Dispose();
                }
            }
            //</snippet17a>
            //<snippet17b>
            using (Font font2 = new Font("Arial", 10.0f))
            {
                byte charset2 = font2.GdiCharSet;
            }
            //</snippet17b>
            //<snippet17c>
            using Font font3 = new Font("Arial", 10.0f);
            byte charset3 = font3.GdiCharSet;

            //</snippet17c>

            //<snippet18>
            Console.Write("Enter a dividend: ");
            int dividend = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter a divisor: ");
            int divisor = Convert.ToInt32(Console.ReadLine());

            if ((divisor != 0) && (dividend / divisor > 0))
            {
                Console.WriteLine("Quotient: {0}", dividend / divisor);
            }
            else
            {
                Console.WriteLine("Attempted division by 0 ends up here.");
            }
            //</snippet18>


            //<snippet19>
            var instance1 = new ExampleClass();
            //</snippet19>
            // Can't show `ExampleClass instance1 = new()` because this projet targets net48.

            //<snippet20>
            ExampleClass instance2 = new ExampleClass();
            //</snippet20>

            //<snippet21a>
            var instance3 = new ExampleClass {
                Name     = "Desktop", ID = 37414,
                Location = "Redmond", Age = 2.3
            };
            //</snippet21a>
            //<snippet21b>
            var instance4 = new ExampleClass();

            instance4.Name     = "Desktop";
            instance4.ID       = 37414;
            instance4.Location = "Redmond";
            instance4.Age      = 2.3;
            //</snippet21b>

            // #22 and #23 are in Coding_Conventions_WF, below.

            // Save 24 in case we add an example to Static Members.

            ExampleClass.totalInstances = 1;

            var customers = new List <Customer>
            {
                new Customer {
                    Name = "Jones", ID = 432, City = "Redmond"
                }
            };

            // Check shop name to use this.
            var distributors = new List <Distributor>
            {
                new Distributor {
                    Name = "ShopSmart", ID = 11302, City = "Redmond"
                }
            };

            //<snippet25>
            //<snippet28>
            var seattleCustomers = from customer in customers
                                   //</snippet28>
                                   where customer.City == "Seattle"
                                   select customer.Name;
            //</snippet25>

            //<snippet26>
            var localDistributors =
                from customer in customers
                join distributor in distributors on customer.City equals distributor.City
                select new { Customer = customer, Distributor = distributor };
            //</snippet26>

            //<snippet27>
            var localDistributors2 =
                from customer in customers
                join distributor in distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };
            //</snippet27>

            //<snippet29>
            var seattleCustomers2 = from customer in customers
                                    where customer.City == "Seattle"
                                    orderby customer.Name
                                    select customer;
            //</snippet29>

            // #30 is in class CompoundFrom

            var customerDistributorNames =
                from customer in customers
                join distributor in distributors on customer.City equals distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            var customerDistributorNames2 =
                from customer in customers
                from distributor in distributors
                where customer.City == distributor.City
                select new { CustomerName = customer.Name, DistributorID = distributor.ID };

            foreach (var c in customerDistributorNames)
            {
                Console.WriteLine(c);
            }

            // Could use in Static Members.
            Console.WriteLine(BaseClass.IncrementTotal());
            // Do not do access the static member of the base class through
            // a derived class.
            Console.WriteLine(DerivedClass.IncrementTotal());

            //// Error
            //var instance = new ExampleClass();
            //Console.WriteLine(instance.IncrementTotal());
        }