예제 #1
0
        public static void Main11(string[] args)
        {
            int    intOne      = 1;
            Int32  intTwo      = 2;
            String stringThree = "3";
            String stringFour  = "4";

            StuffDoer.doStuff(ref intOne, intTwo, ref stringThree, stringFour);
            WriteLine($"{intOne}, {intTwo}, {stringThree}, {stringFour}");
        }
예제 #2
0
        static void Main(string[] args)
        {
            //37
            //decorate property
            WriteLine("Question #37: Decorate property (Attribute)");

            //36
            //registry

            //32
            WriteLine("Question #32: Delegates");
            //e) delegates are ideal for callback functions

            //30
            WriteLine("Question #30: string variable manipulations");
            String s = "NY";

            s.ToUpper();
            s.ToLowerInvariant();
            s.Clone();
            s += "er";
            WriteLine(s);
            //ReadKey();
            s.GetType(); //ctrl + e, h (inheritance hierarchy resharper)

            //29
            WriteLine("Question #29: casting, stack & heap, struts & class");
            Rectangle r = new Rectangle();

            WriteLine(r.Area);
            //ReadKey();

            //25
            //which will compile successfully?

            //24 generics C#

            //23 expando object
            WriteLine("Question #23: Expando object");

            //22
            WriteLine("Question #22: Generics, lambda op (=>), passing methods as parameters, implicit delegate");
            //https://stackoverflow.com/questions/4015451/pass-a-method-as-a-parameter
            Main22();
            //ReadKey();

            //21 again,  HTTP 404 error occurred -- later as //27
            WriteLine("Question #21 umm #27...: inheritance execution order");
            MyTest.Main21Again();
            //ReadKey();

            //21
            WriteLine("Question #21: nullables??");
            City city = new City();

            city.name = "Los Angeles";

            string labelCity = city.name;
            //string labelCountry = city.country.abbrev;

            //a)
            //string labelCountry = null;
            //if(!city.country.abbrev.HasValue)
            //b)
            string labelCountryB = null;

            if (city != null)
            {
                if (city.country == null)
                {
                    labelCountryB = "US";
                }
            }
            ////ReadKey();
            //c
            string labelCountryC = city.country != null ? (city.country.abbrev != null ? city.country.abbrev : "US") : "US";

            //d
            //exception
            //string labelCountryD = city.country.abbrev != null ? city.country.abbrev : "US";

            //e

            string labelCountryE = city?.country?.abbrev ?? "US";

            WriteLine($"Name: {labelCity}");
            WriteLine($"Country: {labelCountryB} {labelCountryC} {labelCountryE}");
            ReadLine();

            //e ??  double question mark is null-coalescing

            //20
            WriteLine("Question #20: More LINQ!");
            int[] values  = new int[] { 5, 4, 3, 4, 2, 5, 9, 4, 2, 4 };
            var   results = (from c in values orderby c select c * c).Distinct().Take(5);

            foreach (var resultt in results)
            {
                WriteLine(resultt);
            }
            WriteLine("--");
            //a
            results = values.Distinct().OrderBy(f => f * f).Take(5).Select(c => c * c);
            foreach (var resultt in results)
            {
                WriteLine(resultt);
            }
            WriteLine("--");
            //b
            IEnumerable <int> resultss = values.Select(c => c * c).Take(5).Distinct().OrderBy(f => f);

            foreach (var resultt in resultss)
            {
                WriteLine(resultt);
            }
            WriteLine("--");
            //c
            IEnumerable <int> resultc = values.Select(c => c * c).OrderBy(f => f).Distinct().Take(5);

            foreach (var resultt in resultc)
            {
                WriteLine(resultt);
            }
            WriteLine("--");
            //d
            IEnumerable <int> resultd = values.Distinct().OrderBy(f => f).Take(5).Select(c => c * c);

            foreach (var resultt in resultd)
            {
                WriteLine(resultt);
            }
            WriteLine("--");
            //cannot implicitly convert type...
            //e
//            int[] resulte = values.Select(c => c * c).Take(5).OrderBy(f => f);
//            foreach (var resultt in resulte)
//            {
//                WriteLine(resultt);
//            }
//            WriteLine("--");
            //ReadKey();


            //19
            ClassTester.Main19();
            //ReadKey();

            //17 -- skipped
            WriteLine("Question #17: System Event Log");
            //EventLogEntry.WriteEvent -- nope
            //EventLog.WriteEntry("asdf");
            //EventInstance.WriteEntry();
            //EventLog.WriteEntry();



            //16 - 1/2
            WriteLine("Question #16: Bitwise Operators");
            //https://stackoverflow.com/questions/4757447/understanding-the-behavior-of-a-single-ampersand-operator-on-integers
            bool?a = null, b = null;

            writeOutput(a & b);
            writeOutput(a | b);
            a = true;
            writeOutput(a & b);
            writeOutput(a | b);
            b = false;
            writeOutput(a & b);
            writeOutput(a | b);
            ReadLine();



            //13
            WriteLine("Question #13: (byte) casting");
            int  intValue  = 250;
            byte byteValue = (byte)intValue;

            WriteLine(byteValue);
            //ReadLine();

            //12
            WriteLine("Question #12: LINQ stuff");
            //https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/introduction-to-linq-queries

            // The Three Parts of a LINQ Query:
            //  1. Data source.
            int[] numbers = new int[7] {
                0, 1, 2, 3, 4, 5, 6
            };

            // 2. Query creation.
            // numQuery is an IEnumerable<int>
            var numQuery =
                from num in numbers
                where (num % 2) == 0
                select num;

            // 3. Query execution.
            foreach (int num in numQuery)
            {
                Console.Write("{0,1} ", num);
            }


            // https://stackoverflow.com/questions/16012380/merge-2-arrays-using-linq
            //join two arrays
            int[] num1 = new int[] { 1, 55, 89, 43, 67, -3 };
            int[] num2 = new int[] { 11, 35, 79, 23, 7, -10 };

            //var result = from n1 in num1
            //  from n2 in num2
            //select result;

            var res    = num1.Concat(num2).ToArray();
            var result = num1.Union(num2).ToArray();

            //var ress = num1.GroupJoin(num2).ToArray();


            //12
            WriteLine("Question #12: LINQ: OrderBy Group CrossJoin Intersect");

            int[] array1 = { 1, 2, 3, 4 };
            int[] array2 = { 3, 5, 5, 8 };
            WriteLine(array1);
            var resss = array1.Intersect(array2).ToArray();

            //var res2 = array1.OrderBy()  --- no
            //var res3 = array1.Group??? --- incrorrect usage
            //var res4 = array1.CrossJoin


            foreach (var item in resss)
            {
                WriteLine(item.ToString());
            }

            //WriteLine(resss);
            //ReadKey();


            //11 2/2
            WriteLine("Question #11: `ref` parameter");
            StuffDoer.Main11(new string[6]);
            //ReadKey();


            //10 - part 2
            WriteLine("Question #10: Derived Classes & Inheritance");

            //B b = new B();
            //A a = new C();
            //E e = new A();
            //C c = new E();
            A aa = new E();

            //7
            WriteLine("Question #7: Queues");
            //Queue<String>() q = new Queue();

            /*
             * tried applying this: https://www.tutorialspoint.com/csharp/csharp_queue.htm
             * wouldn't compile
             *
             * Queue q = new Queue();
             * q.Enqueue('A');
             * q.Enqueue('M');
             * q.Enqueue('G');
             * q.Enqueue('W');
             *
             * WriteLine("Current queue: ");
             * foreach (char c in q) Console.Write(c + " ");
             *
             * WriteLine();
             * q.Enqueue('V');
             * q.Enqueue('H');
             * WriteLine("Current queue: ");
             * foreach (char c in q) Console.Write(c + " ");
             *
             * WriteLine();
             * WriteLine("Removing some values ");
             * char ch = (char)q.Dequeue();
             * WriteLine("The removed value: {0}", ch);
             * ch = (char) q.Dequeue();
             * WriteLine("The removed value: {0}", ch);
             *
             * //ReadKey();
             */


            //5
            var firstName = "";

            WriteLine("Question #5: Exceptions");
            try
            {
            }
            catch (Exception ex) when(firstName == null)
            {
                WriteLine($"{firstName} cannot be null");
            }
            catch (Exception ex)
            {
                if (firstName == null)
                {
                    WriteLine($"{nameof(firstName)} cannot be null");
                }
            }
//            catch (Exception ex) when (firstName == null)
//            {
//                WriteLine($"{nameof(firstName)} cannot be null");
//            }
//            finally (Exception ex)
//            {
//
//            }

            //ReadKey();



            //1
            WriteLine("Question #1: Overriding");
            //which of the following are required to override Equals() method in C#?
            Obj obj = new Obj();

            WriteLine(obj.Equals(obj));

            WriteLine(obj.Equals(null));

            Obj obj1 = new Obj();
            Obj obj2 = new Obj();

            WriteLine(obj1.Equals(obj2));
            ReadKey();
        }