Пример #1
0
        static void Main(string[] args)
        {
            sbyte  valueSByte   = -100;
            byte   valueByte    = 100;
            short  valueShort   = -20000;
            ushort valueUSShort = 50000;
            int    valueInt     = 100000000;
            uint   valueUInt    = 400000000;
            long   valueLong    = -800000000000;
            ulong  valueULong   = 90000000000000000;

            //実数型
            float  valueFloat  = 3.1415F;
            double valueDouble = 3.14159263535;

            //文字型
            char   valueChar   = 'A';
            string valueString = "C# PROGRAM";

            //論理型
            bool valueBool = true;

            Console.WriteLine("{1} :{0}", valueSByte, valueSByte.GetType().Name);
            Console.WriteLine("{1} :{0}", valueByte, valueByte.GetType().Name);
            Console.WriteLine("{1} :{0}", valueShort, valueShort.GetType().Name);
            Console.WriteLine("{1} :{0}", valueUSShort, valueUSShort.GetType().Name);
            Console.WriteLine("{1} :{0}", valueInt, valueInt.GetType().Name);
            Console.WriteLine("{1} :{0}", valueUInt, valueUInt.GetType().Name);
            Console.WriteLine("{1} :{0}", valueLong, valueLong.GetType().Name);
            Console.WriteLine("{1} :{0}", valueULong, valueULong.GetType().Name);
            Console.WriteLine("{1} :{0}", valueFloat, valueFloat.GetType().Name);
        }
Пример #2
0
    private static void ConvertHexToByte()
    {
        // <Snippet4>
        // Create a negative hexadecimal value out of range of the Byte type.
        sbyte  sourceNumber = SByte.MinValue;
        bool   isSigned     = Math.Sign((sbyte)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1;
        string value        = sourceNumber.ToString("X");
        byte   targetNumber;

        try
        {
            targetNumber = Convert.ToByte(value, 16);
            if (isSigned && ((targetNumber & 0x80) != 0))
            {
                throw new OverflowException();
            }
            else
            {
                Console.WriteLine("0x{0} converts to {1}.", value, targetNumber);
            }
        }
        catch (OverflowException)
        {
            Console.WriteLine("Unable to convert '0x{0}' to an unsigned byte.", value);
        }
        // Displays the following to the console:
        //    Unable to convert '0x80' to an unsigned byte.
        // </Snippet4>
    }
        public static void Execute()
        {
            // Declare variables
            ushort unsignedShort = 52130;
            sbyte  shortByte     = -115;
            int    integer       = 4825932;
            byte   _byte         = 97;
            short  _short        = -10000;
            short  _short1       = 20000;
            byte   _byte1        = 224;
            int    integer1      = 970700000;
            byte   _byte2        = 112;
            sbyte  shortByte1    = -44;
            int    integer2      = -1000000;
            short  _short2       = 1990;
            ulong  unsignedLong  = 123456789123456789;

            // Show variables in console
            Console.WriteLine(unsignedShort.GetType() + " : " + unsignedShort.ToString());
            Console.WriteLine(shortByte.GetType() + " : " + shortByte.ToString());
            Console.WriteLine(integer.GetType() + " : " + integer.ToString());
            Console.WriteLine(_byte.GetType() + " : " + _byte.ToString());
            Console.WriteLine(_short.GetType() + " : " + _short.ToString());
            Console.WriteLine(_short1.GetType() + " : " + _short1.ToString());
            Console.WriteLine(_byte1.GetType() + " : " + _byte1.ToString());
            Console.WriteLine(integer1.GetType() + " : " + integer1.ToString());
            Console.WriteLine(_byte2.GetType() + " : " + _byte2.ToString());
            Console.WriteLine(shortByte1.GetType() + " : " + shortByte1.ToString());
            Console.WriteLine(integer2.GetType() + " : " + integer2.ToString());
            Console.WriteLine(_short2.GetType() + " : " + _short2.ToString());
            Console.WriteLine(unsignedLong.GetType() + " : " + unsignedLong.ToString());
        }
Пример #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Source: https://introprogramming.info/english-intro-csharp-book/read-online/chapter-2-primitive-types-and-variables/#_Toc362296391");
            Console.WriteLine("Exercise 1" + Environment.NewLine);

            sbyte num1  = 52;
            byte  num2  = 130;
            sbyte num3  = -115;
            int   num4  = 4825932;
            sbyte num5  = 97;
            short num6  = -10000;
            short num7  = 20000;
            byte  num8  = 224;
            int   num9  = 970700000;
            sbyte num10 = 112;
            sbyte num11 = -44;
            int   num12 = -1000000;
            short num13 = 1990;
            long  num14 = 123456789123456789;

            Console.WriteLine($"Number {num1} it type {num1.GetType()}.");
            Console.WriteLine($"Number {num2} it type {num2.GetType()}.");
            Console.WriteLine($"Number {num3} it type {num3.GetType()}.");
            Console.WriteLine($"Number {num4} it type {num4.GetType()}.");
            Console.WriteLine($"Number {num5} it type {num5.GetType()}.");
            Console.WriteLine($"Number {num6} it type {num6.GetType()}.");
            Console.WriteLine($"Number {num7} it type {num7.GetType()}.");
            Console.WriteLine($"Number {num8} it type {num8.GetType()}.");
            Console.WriteLine($"Number {num9} it type {num9.GetType()}.");
            Console.WriteLine($"Number {num10} it type {num10.GetType()}.");
            Console.WriteLine($"Number {num11} it type {num11.GetType()}.");
            Console.WriteLine($"Number {num12} it type {num12.GetType()}.");
            Console.WriteLine($"Number {num13} it type {num13.GetType()}.");
            Console.WriteLine($"Number {num14} it type {num14.GetType()}.");
        }
Пример #5
0
        public static void Test2()
        {
            byte value = 241;

            //int value = 65546;  //65546 = 65536 + 10
            //int value = 65535;
            //int value = 131071;  //131071 = 65536 + 65535


            //注意在checked上下文(或检查的上下文)中,
            //  强制转换或转换操作尝试执行收缩转换时若
            //    源数据类型的值超出了目标数据类型的范围时:
            //    也会抛出OverflowException异常

            checked //←★试试去掉或加上 checked: 无checked的情形参考Test3()
            {
                try
                {
                    sbyte newValue = (sbyte)value;
                    Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                                      value.GetType().Name, value,
                                      newValue.GetType().Name, newValue);
                }
                catch (OverflowException)
                {
                    Console.WriteLine("Exception: {0} > {1}.", value, SByte.MaxValue);
                }
            }
            // The example displays the following output:
            //       Exception: 241 > 127.
        }
Пример #6
0
        public static void Test3()
        {
            //要使算术运算、强制转换操作或转换操作引发 OverflowException,
            //  操作必须发生在所检查的上下文中。

            //  默认情况下,在 Visual Basic 中会检查数学运算和溢出;
            //            而在 C# 中不检查数学运算和溢出。
            //  如果操作发生在未检查的上下文中,计算的结果被截断。
            //byte value = 241;
            //int value = 65546;  //65546 = 65536 + 10
            //int value = 65535;
            int value = 131071; //131071 = 65536 + 65535

            try
            {
                sbyte newValue = (sbyte)value;
                Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                                  value.GetType().Name, value,
                                  newValue.GetType().Name, newValue);
            }
            catch (OverflowException)
            {
                Console.WriteLine("Exception: {0} > {1}.", value, SByte.MaxValue);
            }
            // The example displays the following output:
            //       Converted the Byte value 241 to the SByte value -15.
        }
Пример #7
0
        public static void Test1()//值类型:整型、浮点型、布尔型、字符型
        {
            #region 整型
            sbyte  sbyteMin = -128, sbyteMax = 127;
            byte   byteMin = 0, byteMax = 255;
            short  shortMin = -32768, shortMax = 32767;
            ushort ushortMin = 0, ushortMax = 65535;
            int    intMin = -2147483648, intMax = 2147483647;
            uint   uintMin = 0, uintMax = 4294967295;
            long   longMin = -9223372036854775808, longMax = 9223372036854775807;
            ulong  ulongMin = 0, ulongMax = 18446744073709551615;
            Console.WriteLine("{0}, {1}, {2}", sbyteMin, sbyteMin.GetType(), typeof(sbyte));
            /* 若超过取值范围会发生数据溢出,得到不正确的结果。*/
            #endregion
            #region 浮点型

            /*小数默认是double类型,double比float精确;
             * decimal用于统计、金融和货币方面的计算,有很高的精确度,但取值范围远小于double。*/
            float   float1 = -123456789.123456789f, float2 = 123456789.123456789f;
            double  double1 = -123456789.123456789, double2 = 123456789.123456789;
            decimal decimal1 = -123456789.123456789m, decimal2 = 123456789.123456789m;
            Console.WriteLine("{0}, {1}, {2}", float1, float1.GetType(), typeof(float));
            #endregion
            #region 布尔型
            bool bool1 = true, bool2 = false;//和其他变量类型不存在转换关系
            Console.WriteLine("{0}, {1}, {2}", bool1, bool1.GetType(), typeof(bool));
            #endregion
            #region 字符型
            char char1 = 'a', char2 = 'b';
            Console.WriteLine("{0}, {1}, {2}", char1, char1.GetType(), typeof(char));
            Console.WriteLine("转义字符:\'  \"");//字符类型还有转义字符
            #endregion
            #region 字符串型
            string        s1 = "Sophie";
            String        s2 = "Sophie";
            StringBuilder s3 = new StringBuilder("Sophie");//用此类对象操作字符串比用string省内存
            string        s4 = " Jill ";

            Console.WriteLine("{0}, {1}, {2}", typeof(string), typeof(string) == typeof(String), typeof(StringBuilder));
            Console.WriteLine("Length: {0}. {1}, {2}. Length2: {3}", s4.Length, s4.TrimEnd('i', ' ', 'e'), s4.TrimStart('S', ' '), s4.Trim().Length);
            Console.WriteLine("{0}, {1}, {2}", String.Compare("a", "b"), String.Compare("b", "a"), String.Compare("a", "a"));                             //-1,1,0
            Console.WriteLine("{0}, {1}, {2}", s1.Equals(s2), s1.Equals("Sophie"), s1.Equals(s3));                                                        //true,true,false
            Console.WriteLine("{0}, {1}, {2}", s1 == s2, s1 == "Sophie", s1.GetType() == s3.GetType());                                                   //true,true,false
            Console.WriteLine("{0}, {1}, {2}", s1.StartsWith("So"), s1.EndsWith("ie"), s1.Contains("ph"));                                                //true,true,true
            Console.WriteLine("{0}, {1}, {2}, {3}, {4}", s4.IndexOf('l'), s4.IndexOf('l', 4), s4.IndexOf('l', 5), s4.IndexOf("ll"), s4.LastIndexOf('l')); //3,4,-1,3,4
            Console.WriteLine("{0}, {1}, {2}, {3}, {4}", s1.Remove(2), s1.Remove(2, 1), s1.Replace("e", "a"), s1.Substring(2), s1.Substring(2, 2));       //So,Sohie,Sophia,phie,ph
            Console.WriteLine("{0}, {1}, {2}, {3}", s1.Insert(0, "2"), s1.Insert(1, "2"), s1.Insert(5, "2"), s1.Insert(6, "2"));                          //2Sophie,S2ophie,Sophi2e,Sophie2
            foreach (string str in "Sophie,Jill,Ada,Claire".Split(','))
            {
                Console.Write(str + " | ");
            }
            Console.WriteLine();
            string[] strs = { "Sophie", "Jill", "Ada", "Claire" };
            Console.WriteLine(String.Join(", ", strs));
            Console.WriteLine("{0}, {1}", s2.PadLeft(12, '0'), s2.PadRight(12, '0'));
            Console.WriteLine(new char[] { 'H', 'a', 'r', 'r', 'y', ' ', 'P', 'o', 't', 't', 'e', 'r' });
            #endregion
            const int CONST1 = 1;//定义常量(非变量,不可二次赋值)
        }
Пример #8
0
        static void Main(string[] args)
        {
            short  a = 123;
            object o = a;
            sbyte  A = (sbyte)(short)o;

            Console.WriteLine(A + " " + A.GetType());
        }
Пример #9
0
        static void Main(string[] args)
        {
            short  forBoxing = 8;
            object boxing    = forBoxing;
            sbyte  unboxing  = Convert.ToSByte(boxing);

            Console.WriteLine(unboxing + "  " + unboxing.GetType());
        }
Пример #10
0
        static void Main(string[] args)
        {
            short  value  = 5;
            object valueO = value;
            sbyte  valueB = (sbyte)(short)valueO;

            Console.WriteLine($"Type :{valueB.GetType()}, Value {valueB} ");
        }
Пример #11
0
 sbyte compareSByte(sbyte b, sbyte b2)
 {
     if (b == b2)
     {
         return(b);
     }
     throw new IllegalStateException("Not got expected value for type: " + b2.GetType().ToString());
 }
Пример #12
0
        static void Task4()
        {
            short num  = 123;
            int   num2 = num;
            sbyte num3 = (sbyte)num2;

            Console.WriteLine("result num: " + num3 + "\n" +
                              "num type " + num3.GetType());
        }
Пример #13
0
        static void Main(string[] args)
        {
            short  a = 150;
            object o = a;
            sbyte  b = (sbyte)(short)o;

            Console.WriteLine(b);
            Console.WriteLine(b.GetType());
        }
Пример #14
0
        static void Main(string[] args)
        {
            short  num1 = 10;
            object obj1 = num1;
            sbyte  num2 = (sbyte)(short)obj1;

            Console.WriteLine(num2);
            Console.WriteLine(num2.GetType());
        }
Пример #15
0
        private void sb_Click(object sender, EventArgs e)
        {
            sbyte sb = 0;

            result  = "ค่า MIN : " + sbyte.MinValue.ToString() + Environment.NewLine;
            result += "ค่า MAX : " + sbyte.MaxValue.ToString() + Environment.NewLine;
            result += "TYPE : " + sb.GetType().ToString();
            MessageBox.Show(result.ToString(), "SByte");
        }
Пример #16
0
            static void GetType()
            {
                sbyte  num             = 0;
                object exampleBoxing   = num;
                sbyte  exampleUnboxing = (sbyte)exampleBoxing;

                Console.WriteLine(exampleUnboxing.GetType());
                Console.WriteLine(exampleUnboxing);
            }
Пример #17
0
        static void Task4()
        {
            short  box     = 23;
            object _short  = box;
            short  unbox   = (short)_short;
            sbyte  nwsByte = (sbyte)unbox;

            Console.WriteLine(nwsByte.GetType());
            Console.WriteLine(unbox);
        }
Пример #18
0
        static void Task4()
        {
            short  firstValue = 5;
            Object obj        = firstValue;

            obj = (sbyte)firstValue;
            sbyte secondValue = (sbyte)obj;

            Console.WriteLine($"{obj} {secondValue.GetType()}");
        }
Пример #19
0
        private static void boxingUnboxing()
        {
            short  shortVal  = 1;
            object objValBox = shortVal;

            Console.WriteLine($"Boxing: {objValBox}, type:{objValBox.GetType()}");
            sbyte sbValUnbox = (sbyte)(short)objValBox;//System.InvalidCastException?!

            Console.WriteLine($"UnBoxing: {sbValUnbox}, type:{sbValUnbox.GetType()}");
        }
Пример #20
0
        static void Main(string[] args)
        {
            Utility.Inizia();

            byte v1 = new byte();

            v1 = 10;
            Console.WriteLine("{0,-16:X}|{1}|{2}", v1.GetHashCode(), v1.GetType(), v1.GetTypeCode());

            sbyte v2 = new sbyte();

            v2 = 11;
            Console.WriteLine("{0,-16:X}|{1}|{2}", v2.GetHashCode(), v2.GetType(), v2.GetTypeCode());

            short v3 = new short();

            v3 = 12;
            Console.WriteLine("{0,-16:X}|{1}|{2}", v3.GetHashCode(), v3.GetType(), v3.GetTypeCode());

            ushort v4 = new ushort();

            v4 = 13;
            Console.WriteLine("{0,-16:X}|{1}|{2}", v4.GetHashCode(), v4.GetType(), v4.GetTypeCode());

            float v5 = new float();

            v5 = 14.0F;
            Console.WriteLine("{0,-16:X}|{1}|{2}", v5.GetHashCode(), v5.GetType(), v5.GetTypeCode());

            double v6 = new double();

            v6 = 14.0D;
            Console.WriteLine("{0,-16:X}|{1}|{2}", v6.GetHashCode(), v6.GetType(), v6.GetTypeCode());

            decimal v7 = new decimal();

            v7 = 15.0M;
            Console.WriteLine("{0,-16:X}|{1}|{2}", v7.GetHashCode(), v7.GetType(), v7.GetTypeCode());

            Console.WriteLine("The size of sbyte   is {0}.", sizeof(sbyte));
            Console.WriteLine("The size of byte    is {0}.", sizeof(byte));
            Console.WriteLine("The size of short   is {0}.", sizeof(short));
            Console.WriteLine("The size of ushort  is {0}.", sizeof(ushort));
            Console.WriteLine("The size of int     is {0}.", sizeof(int));
            Console.WriteLine("The size of uint    is {0}.", sizeof(uint));
            Console.WriteLine("The size of long    is {0}.", sizeof(long));
            Console.WriteLine("The size of ulong   is {0}.", sizeof(ulong));
            Console.WriteLine("The size of char    is {0}.", sizeof(char));
            Console.WriteLine("The size of float   is {0}.", sizeof(float));
            Console.WriteLine("The size of double  is {0}.", sizeof(double));
            Console.WriteLine("The size of decimal is {0}.", sizeof(decimal));
            Console.WriteLine("The size of bool    is {0}.", sizeof(bool));

            Utility.Ferma();
        }
Пример #21
0
        static void Main(string[] args)
        {
            short  x = 10;
            object obj_short;

            obj_short = x;
            sbyte y = (sbyte)(short)obj_short;

            Console.WriteLine(y);
            Console.WriteLine(y.GetType());
        }
Пример #22
0
            public static bool testMethod()
            {
                sbyte b = 0;

                if (b.GetType() == Type.GetType("System.SByte"))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
Пример #23
0
        static void Main(string[] args)
        {
            int a1 = 13;

            System.Int32 a2 = 14;
            sbyte        a3 = 15;

            System.SByte a4 = 16;

            Console.WriteLine("a1: " + a1.GetType());
            Console.WriteLine("a2: " + a2.GetType());
            Console.WriteLine("a3: " + a3.GetType());
            Console.WriteLine("a4: " + a4.GetType());
        }
Пример #24
0
    static void Main()
    {
        ushort firstNumber  = 52130;
        sbyte  secondNumber = -115;
        uint   thirdNumber  = 4825932;
        byte   fourthNumber = 97;
        short  fifthNumber  = -10000;

        Console.WriteLine("First number - {0} ({1})", firstNumber, firstNumber.GetType());
        Console.WriteLine("Second number - {0} ({1})", secondNumber, secondNumber.GetType());
        Console.WriteLine("Third number - {0} ({1})", thirdNumber, thirdNumber.GetType());
        Console.WriteLine("Fourth number - {0} ({1})", fourthNumber, fourthNumber.GetType());
        Console.WriteLine("Fifth number - {0} ({1})", fifthNumber, fifthNumber.GetType());
    }
Пример #25
0
        static void Main(string[] args)
        {
            ushort num1 = 52130;
            sbyte  num2 = -115;
            int    num3 = 4825932;
            byte   num4 = 97;
            short  num5 = -10000;

            Console.WriteLine(num1 + " = " + num1.GetType());
            Console.WriteLine(num2 + " = " + num2.GetType());
            Console.WriteLine(num3 + " = " + num3.GetType());
            Console.WriteLine(num4 + " = " + num4.GetType());
            Console.WriteLine(num5 + " = " + num5.GetType());
        }
Пример #26
0
        static void Main(string[] args)
        {
            //Vereinbarung der Variablen
            byte   byteVariable   = 255;
            sbyte  sbyteVariable  = 1;
            short  int16Variable  = 1;
            ushort uInt16Variable = 1;
            int    int32Variable  = 1;
            uint   uInt32Variable = 1;
            long   int64Variable  = 1;
            ulong  uInt64Variable = 1;

            float  float32Variable  = 1;
            double double64Variable = 1;

            //Ausgabe der Tabelle
            //bitte jeweils in einer Zeile eingeben
            Console.WriteLine("{0}\tvon {1} bis {2}",
                              byteVariable.GetType(), byte.MinValue, byte.MaxValue);
            Console.WriteLine("{0}\tvon {1} bis {2}",
                              sbyteVariable.GetType(), sbyte.MinValue, sbyte.MaxValue);
            Console.WriteLine("{0}\tvon {1} bis {2}",
                              int16Variable.GetType(), short.MinValue, short.MaxValue);
            Console.WriteLine("{0}\tvon {1} bis {2}",
                              uInt16Variable.GetType(), ushort.MinValue, ushort.MaxValue);
            Console.WriteLine("{0}\tvon {1} bis {2}",
                              int32Variable.GetType(), int.MinValue, int.MaxValue);
            Console.WriteLine("{0}\tvon {1} bis {2}",
                              uInt32Variable.GetType(), uint.MinValue, uint.MaxValue);
            Console.WriteLine("{0}\tvon {1} bis {2}",
                              int64Variable.GetType(), long.MinValue, long.MaxValue);
            Console.WriteLine("{0}\tvon {1} bis {2}",
                              uInt64Variable.GetType(), ulong.MinValue, ulong.MaxValue);

            Console.WriteLine("{0}\tvon {1} bis {2}",
                              float32Variable.GetType(), float.MinValue, float.MaxValue);
            Console.WriteLine("{0}\tvon {1} bis {2}",
                              double64Variable.GetType(), double.MinValue, double.MaxValue);



            //bitte in einer Zeile eingeben
            Console.WriteLine("Die Variable hat den Wert: {0}", byteVariable);
            //jetzt erhöhen wir den Wert um 1 über den
            //Inkrement Operator
            byteVariable++;
            //bitte in einer Zeile angeben
            Console.WriteLine("255 + 1 ist gleich {0}????", byteVariable);
        }
Пример #27
0
        static void Main(string[] args)  //注意 Main 大写
        {
            //变量都在System中定义
            //整形
            sbyte sb = 0;                               //8位有符号整数
            short s  = 0;                               //16位有符号整数
            int   i  = 0;                               //32位有符号整数
            long  l  = 0l;                              //64位有符号整数

            byte   b  = 0;                              //8位无符号整数
            ushort us = 0;                              //16位无符号整数
            uint   ui = 0u;                             //32位无符号整数
            ulong  ul = 0ul;                            //64位有符号整数

            Console.WriteLine(sb.GetType().ToString()); // -> System.SByte
            Console.WriteLine(s.GetType().ToString());  // -> System.Int16
            Console.WriteLine(i.GetType().ToString());  // -> System.Int32
            Console.WriteLine(l.GetType().ToString());  // -> System.Int64

            Console.WriteLine(b.GetType().ToString());  // -> System.Byte
            Console.WriteLine(us.GetType().ToString()); // -> System.UInt16
            Console.WriteLine(ui.GetType().ToString()); // -> System.UInt32
            Console.WriteLine(ul.GetType().ToString()); // -> System.UInt64
            //浮点型
            float  f = 0.0f;                            //32位单精度浮点数
            double d = 0.0d;                            //64位双精度浮点数

            Console.WriteLine(f.GetType().ToString());  // -> System.Single
            Console.WriteLine(d.GetType().ToString());  // -> System.Double

            decimal m = 0.0m;                           //128位高精度10进制数表示(常用于货币)

            Console.WriteLine(m.GetType().ToString());  // -> System.Decimal

            bool bo = false;

            Console.WriteLine(bo.GetType().ToString());  // -> System.Boolean

            char c = '\0';

            Console.WriteLine(c.GetType().ToString());  // -> System.Char

            //引用类型
            object obj = c;  //object对象 是 所有对象的 父对象 其类型显示为其引用类型
            string str = "";

            Console.WriteLine(obj.GetType().ToString());  // -> System.Char
            Console.WriteLine(str.GetType().ToString());  // -> System.String
        }
Пример #28
0
    static void Main()
    {
        ushort a = 52130;
        sbyte  b = -115;
        int    c = 4825932;
        byte   d = 97;
        short  e = -10000;

        // I decided to print the variables and their types.
        Console.WriteLine(a + "'s data type is " + a.GetType());
        Console.WriteLine(b + "'s data type is " + b.GetType());
        Console.WriteLine(c + "'s data type is " + c.GetType());
        Console.WriteLine(d + "'s data type is " + d.GetType());
        Console.WriteLine(e + "'s data type is " + e.GetType());
    }
Пример #29
0
    static void Main()
    {
        // Declare variables
        byte   b   = 97;
        sbyte  sb  = -115;
        short  sh  = -10000;
        ushort ush = 52130;
        uint   ui  = 4825932;

        // Write variables and variable system type on the console
        Console.WriteLine(b.GetType().Name + " : " + b);
        Console.WriteLine(sb.GetType().Name + " : " + sb);
        Console.WriteLine(sh.GetType().Name + " : " + sh);
        Console.WriteLine(ush.GetType().Name + " : " + ush);
        Console.WriteLine(ui.GetType().Name + " : " + ui);
    }
Пример #30
0
    private static void Unchecked()
    {
        // <Snippet3>
        byte value = 241;

        try {
            sbyte newValue = (sbyte)value;
            Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                              value.GetType().Name, value,
                              newValue.GetType().Name, newValue);
        }
        catch (OverflowException) {
            Console.WriteLine("Exception: {0} > {1}.", value, SByte.MaxValue);
        }
        // The example displays the following output:
        //       Converted the Byte value 241 to the SByte value -15.
        // </Snippet3>
    }