// Определяет количество значимых цифр после запятой public static int MeaningNumbers(FnLabType input) { FnLabType number = (FnLabType)0.0; string s = ""; int eps = 0; if (number.GetType().ToString() == "System.Double") { s = Math.Abs(input).ToString("F15"); // s = A[i,i] c 15-ю цифрами после запятой s = s.Substring(0, 16); // всего в FnLabType 15 значачих цифр. + 1 запятая. Берем из s только 15 значащих цифр eps = 15 - s.IndexOf(","); // отсюда получаем кол-во значащих цифр после запятой. 15 минус положение "," if (Math.Truncate(Math.Abs(input)) == (FnLabType)0) { eps++; } } else if (number.GetType().ToString() == "System.Single") { s = Math.Abs(input).ToString("F7"); // s = A[i,i] c 7-ю цифрами после запятой s = s.Substring(0, 8); // всего в FnLabType 7 значачих цифр. + 1 запятая. Берем из s только 7 значащих цифр eps = 7 - s.IndexOf(","); // отсюда получаем кол-во значащих цифр после запятой. 7 минус положение "," if (Math.Truncate(Math.Abs(input)) == (FnLabType)0) { eps++; } } return(eps); }
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"); }
void CheckOperators() { { /* * Section 6.21 Types and Operators */ float a = 1.0f; float b = 3.0f; bool c = a > b; // false bool d = a < b; // true Debug.Log("c:" + c + " d:" + d); } { /* * adding strings together */ string a = "hello, "; string b = "world."; string c = a + b; Debug.Log("c: " + c); } { /* * More things you can do with strings */ string a = "hello, "; string b = "world."; bool c = a == b; // false bool d = a == "world."; // true bool e = a == "hello, "; // false bool f = a != b; // true string g = a + b; // hello, world. /* * Some things you can't do with strings */ // string f = a - b; // string f = a * b; // string f = a / b; // bool g = a > b; // bool g = a < b; } { string a = "hello"; switch (a) { case "hello": Debug.Log("a was hello"); break; case "world": Debug.Log("a was world"); break; default: break; } } { /* * Section 6.21.1 GetType() */ int a = 7; string b = "hello"; bool c = a.GetType() == b.GetType(); Debug.Log("a and b are the same type? " + c); // "a and b are the same type? False Debug.Log("a is type: " + a.GetType()); // a is type System.Int32 Debug.Log("b is type: " + b.GetType()); // b is type System.String } { int a = 7; string b = "7"; bool c = a.ToString() == b; Debug.Log("a as a string is the same as b? " + c); // "a as a string is the same as b? True" } { int a = 7; float b = 7.0f; bool c = a == b; Debug.Log("int a == float b? " + c); // "int a == float b? True" } { /* * Section 6.21.2 More Type Casting */ int a = 7; float b = 7.9f; bool c = a == b; // false /* ↑ ↓ ↓ */ /* │ │ └────┐ */ /* │ ┌─┴──┐ ┌─┴──┐ */ /* │ │ 7 │ == │7.9f│ */ /* │ └────┘ ↓ └────┘ */ /* │ ┌───────┐ */ /* └─────┤ False │ */ /* └───────┘ */ bool d = a == (int)b; /* ↑ ↓ ↓ ↓ */ /* │ │ │ ┌─┴──┐ */ /* │ │ │ │7.9f│ */ /* │ │ │ └─┬──┘ */ /* │ │ └─→(int) cast to int */ /* │ │ ↓ */ /* │ ┌─┴──┐ ┌─┴──┐ */ /* │ │ 7 │ == │ 7 │ */ /* │ └────┘ ↓ └────┘ */ /* │ ┌──────┐ */ /* └─────┤ True │ */ /* └──────┘ */ Debug.Log("c: " + c + " d: " + d); // c: False d: True } { int a = 7; string b = "7"; //bool c = a == (int)b; // Cannot convert type 'string' to 'int' } { GameObject a = GameObject.CreatePrimitive(PrimitiveType.Capsule); GameObject b = GameObject.CreatePrimitive(PrimitiveType.Capsule); bool c = a == b; Debug.Log("GameObjects a == b? " + c); // GameObjects a == b? False int a_InstanceID = a.GetInstanceID(); int b_InstanceID = b.GetInstanceID(); Debug.Log("a_InstanceID: " + a_InstanceID); // a_InstanceID: -2500 // though the number itself will change every time you run the game Debug.Log("b_InstanceID: " + b_InstanceID); // b_InstanceID: -2510 // though the number itself will change every time you run the game Debug.Log("GameObjects a_InstanceID == b_InstanceID? " + c); // GameObjects a_InstanceID == b_InstanceID? False } { GameObject a = GameObject.CreatePrimitive(PrimitiveType.Capsule); GameObject b; b = a; bool c = a == b; Debug.Log("GameObject a == b? " + c); // GameObject a == b? True } { GameObject gameObject = GameObject.CreatePrimitive(PrimitiveType.Capsule); int a = gameObject.GetInstanceID(); int b; b = a; bool c = a == b; Debug.Log("InstanceIDs a == b? " + c); // InstanceIDs a == b? True } { /* * Can Types be equal? */ GameObject a = GameObject.CreatePrimitive(PrimitiveType.Capsule); GameObject b = GameObject.CreatePrimitive(PrimitiveType.Capsule); Type aType = a.GetType(); Type bType = b.GetType(); bool c = aType == bType; Debug.Log("aType: " + aType + " bType: " + bType + " aType == bType? " + c); // aType: UnityEngine.GameObject bType: UnityEngine.GameObject aType == bType? True } { GameObject a = GameObject.CreatePrimitive(PrimitiveType.Capsule); a.AddComponent <ZombieComponent>(); // check if the GameObject a is a zombie by looking for the attached zombie component ZombieComponent component = a.GetComponent <ZombieComponent>(); Type b = component.GetType(); Type c = typeof(ZombieComponent); bool d = b == c; Debug.Log("b type: " + b + "c type: " + c + "b == c?" + d); } { System.Int32 a = 1; int b = 3; bool c = a.GetType() == b.GetType(); Debug.Log("a == b? " + c); // "a == b? True System.Single d = 1.0f; float e = 3.0f; bool f = d.GetType() == e.GetType(); Debug.Log("d == e? " + f); // "d == e? True } { /* * 6.21.3 Type Aliasing */ MyOwnType a = 1; Debug.Log("a is a: " + a.GetType()); // "a is a: System.Int16" } { /* * 6.21.4 Boxing and Unboxing */ GameObject a = GameObject.CreatePrimitive(PrimitiveType.Capsule); // remember, HumanWithStates : ZombieWithStates? ZombieWithStates b = a.AddComponent <HumanWithStates>(); /* ↑ ↑ */ /* └────────────────┬─────────────────┘ */ /* ┌──────┴─────────────┐ */ /* │types don't match │ */ /* │but HumanWithStates │ */ /* │inherits from │ */ /* │ZombieWithStates │ */ /* └────────────────────┘ */ GameObject c = GameObject.CreatePrimitive(PrimitiveType.Capsule); MonoBehaviour d = c.AddComponent <ZombieWithStates>(); /* ↑ ↑ */ /* └────────────────┬─────────────────┘ */ /* ┌──────┴─────────────┐ */ /* │types don't match │ */ /* │either but │ */ /* │ZombieWithStates │ */ /* │inherits from │ */ /* │MonoBehaviour │ */ /* └────────────────────┘ */ object e = GameObject.CreatePrimitive(PrimitiveType.Capsule); /* ↑ ↑ */ /* └─────┬─────┘ */ /* ┌──────┴─────────────┐ */ /* │GameObject inherits │ */ /* │from object │ */ /* │but every class in │ */ /* │C# inherits from │ */ /* │object! │ */ /* └────────────────────┘ */ object f = 1; object g = 1.0f; object h = new object[] { 1, 1.0f, e, d, c, b, a }; Debug.Log("what is h really? " + h); // what is h really? System.Object[] object[] objArray = h as object[]; foreach (object o in objArray) { if (o is GameObject) { Debug.Log("got a GameObject!"); } } // got a GameObject! // got a GameObject! // got a GameObject! UnityEngine.Object[] allObjectsInTheScene = FindObjectsOfType <UnityEngine.Object>(); foreach (UnityEngine.Object o in allObjectsInTheScene) { Debug.Log("Object: " + o); } // all kinds of things! } }
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); }