コード例 #1
0
            public static void Main()
            {
                object b = new Giraffe();

                Console.WriteLine(b is Animal);                    // output: True
                Console.WriteLine(b.GetType() == typeof(Animal));  // output: False

                Console.WriteLine(b is Giraffe);                   // output: True
                Console.WriteLine(b.GetType() == typeof(Giraffe)); // output: True
            }
コード例 #2
0
        public static void Main()
        {
            object b = new Giraffe();

            Console.WriteLine("b is Animal: {0}", b is Animal);
            Console.WriteLine("b.GetType() == typeof(Animal): {0}", b.GetType() == typeof(Animal));

            Console.WriteLine("--------------------------------------");

            Console.WriteLine("b is Giraffe: {0}", b is Giraffe);
            Console.WriteLine("b.GetType() == typeof(Giraffe): {0}", b.GetType() == typeof(Giraffe));
        }
コード例 #3
0
        static void Main(string[] args)
        {
            object ab = 32.1;                //double
            object ad = 1;                   //this is integer

            Console.WriteLine(ab is double); // output: True
            Console.WriteLine(ab is int);    // output: False

            Console.WriteLine(ad is int);    // output: True
            Console.WriteLine(ab is double); // output: false

            Console.WriteLine("\n");

            object b = new Base();

            Console.WriteLine(b is Base);    // output: True
            Console.WriteLine(b is Derived); // output: False

            object d = new Derived();

            Console.WriteLine(d is Base);    // output: True
            Console.WriteLine(d is Derived); // output: True

            Console.WriteLine("\n");

            int i = 27;

            Console.WriteLine(i is System.IFormattable); // output: True

            object iBoxed = i;                           //when you turn something into an object class instance it is boxing

            Console.WriteLine(iBoxed is int);            // output: True
            Console.WriteLine(iBoxed is long);           // output: False

            Console.WriteLine("\n");

            i      = 23;
            iBoxed = i;
            int?jNullable = 7;

            if (iBoxed is int newint1 && jNullable is int newint2)
            {
                Console.WriteLine(newint1 + newint2);  // output 30
            }

            if (iBoxed is double newint_1 && jNullable is int newint_2)
            {
                Console.WriteLine(newint_1 + newint_2);  // this wont print since this if statement will fail
            }

            Console.WriteLine("\n");

            IEnumerable <int> numbers   = new[] { 10, 20, 30 };
            IList <int>       indexable = numbers as IList <int>;

            if (indexable != null)
            {
                Console.WriteLine(indexable[0] + indexable[indexable.Count - 1]);  // output: 40
            }

            double x = 1234.7;
            int    a = (int)x;

            Console.WriteLine(a);   // output: 1234,

            x = 1234324324234.7;
            a = (int)x;
            Console.WriteLine(a);   // output: -2147483648

            numbers = new int[] { 10, 20, 30 };
            IList <int> list = (IList <int>)numbers;

            Console.WriteLine(list.Count); // output: 3
            Console.WriteLine(list[1]);    // output: 20

            Console.WriteLine("\n");

            void PrintType <T>() => Console.WriteLine(typeof(T));//this is inline method

            Console.WriteLine(typeof(List <string>));
            PrintType <int>();
            PrintType <System.Int32>();
            PrintType <Dictionary <int, char> >();

            b = new Giraffe();
            Console.WriteLine(b is Animal);                    // output: True
            Console.WriteLine(b.GetType() == typeof(Animal));  // output: False

            Console.WriteLine(b is Giraffe);                   // output: True
            Console.WriteLine(b.GetType() == typeof(Giraffe)); // output: True

            double        d1      = 3215324324.123123;
            long          l1      = 432543534534543543;
            List <string> list111 = new List <string> {
                "323", "1"
            };                                                     //this is managed type.


            Console.WriteLine(sizeof(byte));   // output: 1
            Console.WriteLine(sizeof(double)); // output: 8


            List <customClassSizeTest> lstTest = new List <customClassSizeTest>();

            var mem1 = GC.GetTotalMemory(true);

            for (int mm = 0; mm < 400; mm++)
            {
                lstTest.Add(new customClassSizeTest(1, 3215315, 54654654654, 32));
            }

            var mem2 = GC.GetTotalMemory(true);

            Console.WriteLine((mem2 - mem1) + $" bytes , avg {((mem2 - mem1)/400).ToString("N0")} is the size of customClassSizeTest");  // doesnt work
        }