static void Main(string[] args) { sbyte a1 = 1; System.SByte a2 = 1; byte b1 = 2; System.Byte b2 = 2; short c1 = 3; System.Int16 c2 = 3; ushort d1 = 4; System.UInt16 d2 = 4; int e1 = 5; System.Int32 e2 = 5; uint f1 = 6; System.UInt32 f2 = 6; long g1 = 7; System.Int64 g2 = 7; ulong h1 = 8; System.UInt64 h2 = 8; char i1 = 'a'; System.Char i2 = 'a'; float k1 = 10; System.Single k2 = 10; double l1 = 11; System.Double l2 = 11; bool m1 = true; System.Boolean m2 = true; decimal n1 = 12; System.Decimal n2 = 12; object o1 = new object { }; System.Object o2 = new object { }; string p1 = "Hello"; System.String p2 = "Hello"; Console.WriteLine(a1.GetType() + "\n" + a2.GetType() + "\n" + b1.GetType() + "\n" + b2.GetType() + "\n" + c1.GetType() + "\n" + c2.GetType() + "\n" + d1.GetType() + "\n" + d2.GetType() + "\n" + e1.GetType() + "\n" + e2.GetType() + "\n" + f1.GetType() + "\n" + f2.GetType() + "\n" + g1.GetType() + "\n" + g2.GetType() + "\n" + h1.GetType() + "\n" + h2.GetType() + "\n" + i1.GetType() + "\n" + i2.GetType() + "\n" + k1.GetType() + "\n" + k2.GetType() + "\n" + l1.GetType() + "\n" + l2.GetType() + "\n" + m1.GetType() + "\n" + m2.GetType() + "\n" + n1.GetType() + "\n" + n2.GetType() + "\n" + o1.GetType() + "\n" + o2.GetType() + "\n" + p1.GetType() + "\n" + p2.GetType() + "\n"); }
static void Main(string[] args) { Console.WriteLine("***** Data Types *****"); // 12 is-a System.Int32 which is-a System.ValueType // which is-a System.Object. Console.WriteLine(12.GetHashCode()); Console.WriteLine(12.Equals(23)); Console.WriteLine(12.GetType().BaseType); Console.WriteLine(12.ToString()); Console.WriteLine(12); // ToString() called automatically. // Exercise a ushort. Console.WriteLine("\n***** Fun with unsigned shorts *****"); System.UInt16 myUInt16 = 30000; Console.WriteLine("Max for an UInt16 is: {0} ", UInt16.MaxValue); Console.WriteLine("Min for an UInt16 is: {0} ", UInt16.MinValue); Console.WriteLine("Value is: {0} ", myUInt16); Console.WriteLine("I am a: {0} ", myUInt16.GetType()); // Now in System.UInt16 shorthand (e.g. a ushort). ushort myOtherUInt16 = 12000; Console.WriteLine("Max for an UInt16 is: {0} ", ushort.MaxValue); Console.WriteLine("Min for an UInt16 is: {0} ", ushort.MinValue); Console.WriteLine("Value is: {0} ", myOtherUInt16); Console.WriteLine("I am a: {0} ", myOtherUInt16.GetType()); Console.WriteLine("\n***** Fun with doubles *****"); Console.WriteLine("-> double.Epsilon: {0}", double.Epsilon); Console.WriteLine("-> double.PositiveInfinity: {0}", double.PositiveInfinity); Console.WriteLine("-> double.NegativeInfinity: {0}", double.NegativeInfinity); Console.WriteLine("-> double.MaxValue: {0}", double.MaxValue); Console.WriteLine("-> double.MinValue: {0}", double.MinValue); Console.WriteLine("\n***** Fun with bools *****"); // No more ad-hoc Boolean types in C#! // bool b = 0; // Illegal! // bool b2 = -1; // Also illegal! bool b3 = true; // No problem. bool b4 = false; // No problem. Console.WriteLine("-> bool.FalseString: {0}", bool.FalseString); Console.WriteLine("-> bool.TrueString: {0}", bool.TrueString); // Test the truth of the following statements... Console.WriteLine("\n***** Fun with chars *****"); Console.WriteLine("-> char.IsDigit('K'): {0}", char.IsDigit('K')); Console.WriteLine("-> char.IsDigit('9'): {0}", char.IsDigit('9')); Console.WriteLine("-> char.IsLetter('10', 1): {0}", char.IsLetter("10", 1)); Console.WriteLine("-> char.IsLetter('p'): {0}", char.IsLetter('p')); Console.WriteLine("-> char.IsWhiteSpace('Hello There', 5): {0}", char.IsWhiteSpace("Hello There", 5)); Console.WriteLine("-> char.IsWhiteSpace('Hello There', 6): {0}", char.IsWhiteSpace("Hello There", 6)); Console.WriteLine("-> char.IsLetterOrDigit('?'): {0}", char.IsLetterOrDigit('?')); Console.WriteLine("-> char.IsPunctuation('!'): {0}", char.IsPunctuation('!')); Console.WriteLine("-> char.IsPunctuation('>'): {0}", char.IsPunctuation('>')); Console.WriteLine("-> char.IsPunctuation(','): {0}", char.IsPunctuation(',')); // Test the truth of the following statements... Console.WriteLine("\n***** Fun with parsing *****"); bool myBool = bool.Parse("True"); Console.WriteLine("-> Value of myBool: {0}", myBool); double myDbl = double.Parse("99.884"); Console.WriteLine("-> Value of myDbl: {0}", myDbl); int myInt = int.Parse("8"); Console.WriteLine("-> Value of myInt: {0}", myInt); char myChar = char.Parse("w"); Console.WriteLine("-> Value of myChar: {0}\n", myChar); Console.WriteLine("\n***** Fun with DateTime *****"); // This constructor takes (year, month, day) DateTime dt = new DateTime(2004, 10, 17); // What day of the month is this? Console.WriteLine("The day of {0} is {1}", dt.Date, dt.DayOfWeek); dt.AddMonths(2); // Month is now December. Console.WriteLine("Daylight savings: {0}", dt.IsDaylightSavingTime()); Console.WriteLine("\n***** Fun with TimeSpan *****"); // This constructor takes (hrs, minutes, seconds) TimeSpan ts = new TimeSpan(4, 30, 0); Console.WriteLine(ts); // Subtract 15 minutes from the current TimeSpan and // print the result. Console.WriteLine(ts.Subtract(new TimeSpan(0, 15, 0))); Console.ReadLine(); }
static void Main(string[] args) { bool a = true; System.Boolean a1 = false; System.Type[] a2 = { a.GetType(), a1.GetType() }; foreach (var item in a2) { Console.Write(item.ToString() + "\n"); } byte b = 10; System.Byte b1 = 11; System.Type[] b2 = { b.GetType(), b1.GetType() }; foreach (var item in b2) { Console.Write(item.ToString() + "\n"); } sbyte c = 22; System.SByte c1 = 23; System.Type[] c2 = { c.GetType(), c1.GetType() }; foreach (var item in c2) { Console.Write(item.ToString() + "\n"); } short d = -33; System.Int16 d1 = -34; System.Type[] d2 = { d.GetType(), d1.GetType() }; foreach (var item in d2) { Console.Write(item.ToString() + "\n"); } ushort e = 33; System.UInt16 e1 = 34; System.Type[] e2 = { e.GetType(), e1.GetType() }; foreach (var item in e2) { Console.Write(item.ToString() + "\n"); } int f = 44; System.Int32 f1 = 45; System.Type[] f2 = { f.GetType(), f1.GetType() }; foreach (var item in f2) { Console.Write(item.ToString() + "\n"); } uint g = 55; System.UInt32 g1 = 56; System.Type[] g2 = { g.GetType(), g1.GetType() }; foreach (var item in g2) { Console.Write(item.ToString() + "\n"); } long h = -555; System.Int64 h1 = -556; System.Type[] h2 = { h.GetType(), h1.GetType() }; foreach (var item in h2) { Console.Write(item.ToString() + "\n"); } ulong i = 555; System.UInt64 i1 = 556; System.Type[] i2 = { i.GetType(), i1.GetType() }; foreach (var item in i2) { Console.Write(item.ToString() + "\n"); } float j = 666; System.Single j1 = 667; System.Type[] j2 = { j.GetType(), j1.GetType() }; foreach (var item in j2) { Console.Write(item.ToString() + "\n"); } double k = 777; System.Double k1 = 778; System.Type[] k2 = { k.GetType(), k1.GetType() }; foreach (var item in k2) { Console.Write(item.ToString() + "\n"); } decimal l = 888; System.Decimal l1 = 889; System.Type[] l2 = { l.GetType(), l1.GetType() }; foreach (var item in l2) { Console.Write(item.ToString() + "\n"); } char m = 'a'; System.Char m1 = 'b'; System.Type[] m2 = { m.GetType(), m1.GetType() }; foreach (var item in m2) { Console.Write(item.ToString() + "\n"); } string n = "Hello"; System.String n1 = "Bye"; System.Type[] n2 = { n.GetType(), n1.GetType() }; foreach (var item in n2) { Console.Write(item.ToString() + "\n"); } Console.ReadLine(); }
static void Main(string[] args) { // объявляю все переменные двумя способами sbyte a1 = 1; System.SByte a2 = 2; short b1 = 3; System.Int16 b2 = 4; int c1 = 5; System.Int32 c2 = 6; long d1 = 7; System.Int64 d2 = 8; byte e1 = 9; System.Byte e2 = 10; ushort f1 = 11; System.UInt16 f2 = 12; uint g1 = 13; System.UInt32 g2 = 14; ulong h1 = 15; System.UInt64 h2 = 16; char i1 = 'A'; System.Char i2 = 'B'; float j1 = 17.1F; System.Single j2 = 17.2F; double k1 = 17.11; System.Double k2 = 17.22; bool l1 = false; System.Boolean l2 = true; decimal m1 = 1.1M; System.Decimal m2 = 2.2M; object n1 = 18; System.Object n2 = 19; string o1 = "Hi!"; System.String o2 = "Bye!"; //получаю типы этих переменных Type nameA1 = a1.GetType(); Type nameA2 = a2.GetType(); Type nameB1 = b1.GetType(); Type nameB2 = b2.GetType(); Type nameC1 = c1.GetType(); Type nameC2 = c2.GetType(); Type nameD1 = d1.GetType(); Type nameD2 = d2.GetType(); Type nameE1 = e1.GetType(); Type nameE2 = e2.GetType(); Type nameF1 = f1.GetType(); Type nameF2 = f2.GetType(); Type nameG1 = g1.GetType(); Type nameG2 = g2.GetType(); Type nameH1 = h1.GetType(); Type nameH2 = h2.GetType(); Type nameI1 = i1.GetType(); Type nameI2 = i2.GetType(); Type nameJ1 = j1.GetType(); Type nameJ2 = j2.GetType(); Type nameK1 = k1.GetType(); Type nameK2 = k2.GetType(); Type nameL1 = l1.GetType(); Type nameL2 = l2.GetType(); Type nameM1 = m1.GetType(); Type nameM2 = m2.GetType(); Type nameN1 = n1.GetType(); Type nameN2 = n2.GetType(); Type nameO1 = o1.GetType(); Type nameO2 = o2.GetType(); //вывожу типы переменных на консоль Console.WriteLine(nameA1); Console.WriteLine(nameA2); Console.WriteLine(nameB1); Console.WriteLine(nameB2); Console.WriteLine(nameC1); Console.WriteLine(nameC2); Console.WriteLine(nameD1); Console.WriteLine(nameD2); Console.WriteLine(nameE1); Console.WriteLine(nameE2); Console.WriteLine(nameF1); Console.WriteLine(nameF2); Console.WriteLine(nameG1); Console.WriteLine(nameG2); Console.WriteLine(nameH1); Console.WriteLine(nameH2); Console.WriteLine(nameI1); Console.WriteLine(nameI2); Console.WriteLine(nameJ1); Console.WriteLine(nameJ2); Console.WriteLine(nameK1); Console.WriteLine(nameK2); Console.WriteLine(nameL1); Console.WriteLine(nameL2); Console.WriteLine(nameM1); Console.WriteLine(nameM2); Console.WriteLine(nameN1); Console.WriteLine(nameN2); Console.WriteLine(nameO1); Console.WriteLine(nameO2); }
static void Main() { /* * prefix s - simple type, primitive type, built in type, value type * prefix f - .NET Framework type * * Numeric Types */ // signed 8 bit integer sbyte sByte = -128; System.SByte fByte = 127; // unsigned 8 bit integer byte sUnByte = 0; System.Byte fUnByte = 255; // signed 16 bit integer short sShort = -32768; System.Int16 fShort = 32767; // unsigned 16 bit integer ushort sUnShort = 0; System.UInt16 fUnShort = 65535; // signed 32 bit integer int sInt = -2147483648; System.Int32 fInt = 2_147_483_647; // unsigned 32 bit integer uint sUnInt = 0; System.UInt32 fUnInt = 4_294_967_295; // signed 64 bit integer long sLong = -9_223_372_036_854_775_808; System.Int64 fLong = 0x7FFFFFFFFFFFFFFF; // unsigned 64 bit integer ulong sUnLong = 0; System.UInt64 fUnLong = System.UInt64.MaxValue; // float 32 bit floating-point value (to initialize float use suffix 'f' or 'F') float sFloat = -3.5F; System.Single fFloat = 3.5f; // double 64 bit floating-point value double sDouble = System.Double.MinValue; System.Double fDouble = System.Double.MaxValue; // decimal 128 bit floating-point value (financial calculations)(use suffix m for real numbers) decimal sDecimal = 300.5m; System.Decimal fDecimal = 300; // character data type char sChar = 'a'; System.Char fChar = 'z'; // boolean type bool trueValue = true; bool falseValue = false; // default value for bool type // write to console Console.WriteLine("Type: {0}, Value: {1}", sByte.GetType(), sByte); Console.WriteLine("Type: {0}, Value: {1}", fByte.GetType(), fByte); Console.WriteLine("Type: {0}, Value: {1}", sUnByte.GetType(), sUnByte); Console.WriteLine("Type: {0}, Value: {1}", fUnByte.GetType(), fUnByte); Console.WriteLine("Type: {0}, Value: {1}", sShort.GetType(), sShort); Console.WriteLine("Type: {0}, Value: {1}", fShort.GetType(), fShort); Console.WriteLine("Type: {0}, Value: {1}", sUnShort.GetType(), sUnShort); Console.WriteLine("Type: {0}, Value: {1}", fUnShort.GetType(), fUnShort); Console.WriteLine("Type: {0}, Value: {1}", sInt.GetType(), sInt); Console.WriteLine("Type: {0}, Value: {1}", fInt.GetType(), fInt); Console.WriteLine("Type: {0}, Value: {1}", sUnInt.GetType(), sUnInt); Console.WriteLine("Type: {0}, Value: {1}", fUnInt.GetType(), fUnInt); Console.WriteLine("Type: {0}, Value: {1}", sLong.GetType(), sLong); Console.WriteLine("Type: {0}, Value: {1}", fLong.GetType(), fLong); Console.WriteLine("Type: {0}, Value: {1}", sUnLong.GetType(), sUnLong); Console.WriteLine("Type: {0}, Value: {1}", fUnLong.GetType(), fUnLong); Console.WriteLine("Type: {0}, Value: {1}", sFloat.GetType(), sFloat); Console.WriteLine("Type: {0}, Value: {1}", fFloat.GetType(), fFloat); Console.WriteLine("Type: {0}, Value: {1}", sDouble.GetType(), sDouble); Console.WriteLine("Type: {0}, Value: {1}", fDouble.GetType(), fDouble); Console.WriteLine("Type: {0}, Value: {1}", sDecimal.GetType(), sDecimal); Console.WriteLine("Type: {0}, Value: {1}", fDecimal.GetType(), fDecimal); Console.WriteLine("Type: {0}, Value: {1}", sChar.GetType(), sChar); Console.WriteLine("Type: {0}, Value: {1}", fChar.GetType(), fChar); Console.WriteLine("Type: {0}, Value: {1}", trueValue.GetType(), trueValue); Console.WriteLine("Type: {0}, Value: {1}", falseValue.GetType(), falseValue); // wait for user input to close the window Console.WriteLine(); Console.Write("Press any key to continue..."); Console.ReadKey(true); }
public static int Main(string[] args) { // Test value semantics. System.Int32 intA = 1001; System.Int32 intB = 1000; if (intA == intB) { Console.WriteLine("Same value!\n"); } else { Console.WriteLine("Not the same value!\n"); } // Working with UInt16 as a wrapper. System.UInt16 myUInt16 = 30000; Console.WriteLine("Max for an UInt16 is: {0}", UInt16.MaxValue); Console.WriteLine("Min for an UInt16 is: {0}", UInt16.MinValue); Console.WriteLine("Your value is: {0}", myUInt16.ToString()); Console.WriteLine("I am a: {0}", myUInt16.GetType().ToString()); // Now in UInt16 shorthand (e.g. a ushort). ushort myOtherUInt16 = 12000; Console.WriteLine("\nYour value is: {0}", myOtherUInt16.ToString()); Console.WriteLine("I am a: {0}", myOtherUInt16.GetType().ToString()); // Boxing and unboxing. Console.WriteLine("\nNow for some boxing..."); // Make a simple value. short s = 25; Console.WriteLine("short s = {0}", s); Console.WriteLine("short is a: {0}\n", s.GetType().ToString()); // Box the value into a reference. object objShort = s; Console.WriteLine("Boxed object is a: {0}\n", objShort.GetType().ToString()); // Now, unbox the reference back into a short: short anotherShort = (short)objShort; Console.WriteLine("short anotherShort = {0}", anotherShort); Console.WriteLine("Unboxed object is a: {0}", anotherShort.GetType().ToString()); // Bad unboxing! try { // The type contained in the box is NOT a // string, but a short! string str = (string)objShort; } catch (InvalidCastException e) { Console.WriteLine("\nOOPS!\n{0}", e.ToString()); } // Auto-box. Console.WriteLine("\n\nThe Auto Box"); int x = 99; Foo(x); return(0); }