Core C#

Type Operators

This C# program demonstrates how the type-based operators are used.

Program.cs

using System;

namespace XoaX {
    class Program {
        public class CMan {
            public uint muiAge;
            public string msName;
        }

        public class CPriest : CMan {
            public uint muiYearOrdained;
        }

        static void Main(string[] args) {
            int iMyInt = 1303515112;
            Console.WriteLine("The int type");
            Console.WriteLine(typeof(int) + " is the type of int");
            Console.WriteLine(iMyInt.GetType() + " is the type of iMyInt");
            Console.WriteLine(default(int) + " is the default value of int");
            Console.WriteLine(sizeof(int) + " is the size of an int in bytes");
            Console.WriteLine();

            double dX = 39.73;
            Console.WriteLine("The double type");
            Console.WriteLine(typeof(double) + " is the type of double");
            Console.WriteLine(dX.GetType() + " is the type of dX");
            Console.WriteLine(default(double) + " is the default value of double");
            Console.WriteLine(sizeof(double) + " is the size of a double in bytes");
            Console.WriteLine(dX + " is the value of dX");
            Console.WriteLine((int)dX + " is the value of dX after being cast to an int");
            Console.WriteLine();

            CMan qAdam = new CMan() {muiAge = 89, msName = "Adam"};
            CPriest qPeter = new CPriest() { muiAge = 89, msName = "Adam", muiYearOrdained = 32 };
            Console.WriteLine("Reference types");
            Console.WriteLine(typeof(CMan) + " is the type of CMan");
            Console.WriteLine(typeof(CPriest) + " is the type of CPriest");
            Console.WriteLine("null " + ((default(CMan) == null) ? "is": "is not") +
                " the default value of CMan");
            Console.WriteLine();

            Console.WriteLine("The as operator returns a reference");
            Console.WriteLine("(qPeter as CMan)" +
                (((qPeter as CMan) == null) ? " is " : " is not ") + "null");
            Console.WriteLine("(qAdam as CPriest)" +
                (((qAdam as CPriest) == null) ? " is " : " is not ") + "null");
            Console.WriteLine();

            Console.WriteLine("The is operator returns a bool");
            Console.WriteLine("(qAdam is CMan) = " + (qAdam is CMan));
            Console.WriteLine("(qAdam is CPriest) = " + (qAdam is CPriest));
            Console.WriteLine("(qPeter is CMan) = " + (qPeter is CMan));
            Console.WriteLine("(qPeter is CPriest) = " + (qPeter is CPriest));
        }
    }
}
 

Output

The int type
System.Int32 is the type of int
System.Int32 is the type of iMyInt
0 is the default value of int
4 is the size of an int in bytes

The double type
System.Double is the type of double
System.Double is the type of dX
0 is the default value of double
8 is the size of a double in bytes
39.73 is the value of dX
39 is the value of dX after being cast to an int

Reference types
XoaX.Program+CMan is the type of CMan
XoaX.Program+CPriest is the type of CPriest
null is the default value of CMan

The as operator returns a reference
(qPeter as CMan) is not null
(qAdam as CPriest) is null

The is operator returns a bool
(qAdam is CMan) = True
(qAdam is CPriest) = False
(qPeter is CMan) = True
(qPeter is CPriest) = True
Press any key to continue . . .
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.