public static int EnumtoInt(MyValueType DataType) { if (DataType == MyValueType.Int) { return(1); } if (DataType == MyValueType.Decimal) { return(2); } if (DataType == MyValueType.String) { return(3); } if (DataType == MyValueType.Date) { return(4); } if (DataType == MyValueType.DateTime) { return(5); } if (DataType == MyValueType.Bool) { return(6); } return(0); }
static void RecursiveAction(MyValueType i) { //Console.WriteLine(i.counter); if (i.counter <= 0) { return; } i.counter--; RecursiveAction(i); }
private static void StructMemberAccess(MyValueType valueType) { valueType.Field = 0; valueType.Field += 5; valueType.Field[1] = 5; valueType.Field.CallMe(); DynamicTests.Casts(valueType.GetOnlyProperty); valueType.GetOnlyProperty.CallMe(); valueType.Property = 0; valueType.Property += 5; valueType.Property[1] = 5; valueType.Property.CallMe(5.ToDynamic((object)valueType.Property.Call())); valueType.Method(valueType.GetOnlyProperty + valueType.Field); }
static void Main(string[] args) { // Value Type MyValueType myValueType = new MyValueType(); myValueType.X = 999999999999999; Console.WriteLine(myValueType.Multiply()); // Reference Type MyReferenceType myReferenceType = new MyReferenceType(); myReferenceType.X = 999999999999999; Console.WriteLine(myReferenceType.Multiply()); Console.ReadLine(); }
static void Main(string[] args) { // class MyreferenceType MyReferenceType a1 = new MyReferenceType(); // 変数を宣言 a1.X = 123; // 123を入れる MyReferenceType a2 = a1; // 複製を作る a1.X = 456; // 456を入れる Console.WriteLine("a1.X={0}", a1.X); Console.WriteLine("a2.X={0}", a2.X); // struct MyValueType MyValueType b1 = new MyValueType(); // 変数を宣言 b1.X = 123; // 123を入れる MyValueType b2 = b1; // 複製を作る b1.X = 456; // 456を入れる Console.WriteLine("b1.X={0}", b1.X); Console.WriteLine("b2.X={0}", b2.X); }
public Value(MyValueType type, byte[] data, int startIndex = 0) { this.type = type; switch (type) { case MyValueType.INTEGER: this.data = BitConverter.ToInt32(data, startIndex); break; case MyValueType.COLOR: this.data = Color.FromArgb(BitConverter.ToInt32(data, startIndex)); break; case MyValueType.STRING: this.data = Encoding.ASCII.GetString(data); break; default: this.data = null; break; } }
public Value(string charString) { type = MyValueType.STRING; data = charString; }
public Value(int integer) { type = MyValueType.INTEGER; data = integer; }
public Value(Color color) { type = MyValueType.COLOR; data = color; }
public bool Equals(MyValueType other) { return(_value == other._value); }
public FooNonGeneric(MyValueType v) { _Value = v; }
private void CheckCopyTo(Array expectedCopyTo, bool expectCopyToItemsOutOfOrder, bool copyToOnlySupportsZeroLowerBounds) { Object[] arrayToCopyTo = null; T[] tArrayToCopyTo = new T[expectedCopyTo.Length]; Object[,] arrayToCopyToMulti = new Object[1, 1]; MyFooType[] badCastReferenceTypeArray = new MyFooType[expectedCopyTo.Length]; MyValueType[] badCastValueTypeArray = new MyValueType[expectedCopyTo.Length]; try { m_pCollectionToTest.CopyTo(arrayToCopyTo, 0); m_test.Eval(false, "Expected ArgumentNullException when attempting to copy to null Array from ICollection."); } catch (ArgumentNullException) { } catch (Exception E) { m_test.Eval(false, "Unknown Exception when attempting to copy to null Array from ICollection: " + E); } try { m_pCollectionToTest.CopyTo(arrayToCopyToMulti, 0); m_test.Eval(false, "Expected ArgumentException when attempting to copy to Multidimensional Array from ICollection."); } catch (ArgumentException) { } catch (Exception E) { m_test.Eval(false, "Unknown Exception when attempting to copy to Multidimensional Array from ICollection: " + E); } arrayToCopyTo = new Object[expectedCopyTo.Length - 1]; try { m_pCollectionToTest.CopyTo(arrayToCopyTo, 0); m_test.Eval(false, "Expected ArgumentException when attempting to copy to smaller Array from ICollection."); } catch (ArgumentException) { } catch (Exception E) { m_test.Eval(false, "Unknown Exception when attempting to copy to smaller Array from ICollection: " + E); } arrayToCopyTo = new Object[expectedCopyTo.Length]; try { m_pCollectionToTest.CopyTo(arrayToCopyTo, expectedCopyTo.Length); m_test.Eval(false, "Expected ArgumentException when attempting to copy to array at index >= Length of Array from ICollection."); } catch (ArgumentException) { } catch (Exception E) { m_test.Eval(false, "Unknown Exception when attempting to copy to array at index >= Length of Array from ICollection: " + E); } try { m_pCollectionToTest.CopyTo(arrayToCopyTo, -1); m_test.Eval(false, "Expected ArgumentOutOfRangeException when attempting to copy to array at index < 0 from ICollection."); } catch (ArgumentOutOfRangeException) { } catch (Exception E) { m_test.Eval(false, "Unknown Exception when attempting to copy to array at index < 0 from ICollection: " + E); } try { m_pCollectionToTest.CopyTo(arrayToCopyTo, 1); m_test.Eval(false, "Expected ArgumentException when attempting to copy to array without enough room between index and end of array from ICollection."); } catch (ArgumentException) { } catch (Exception E) { m_test.Eval(false, "Unknown Exception when attempting to copy to array without enough room between index and end of array from ICollection: " + E); } try { m_pCollectionToTest.CopyTo(badCastReferenceTypeArray, 0); m_test.Eval(false, "Expected ArrayTypeMismatchException when attempting to copy to array that cannot be cast to from ICollection."); } catch (ArgumentException) { } catch (Exception E) { m_test.Eval(false, "Unknown Exception when attempting to copy to reference type array that cannot be cast to from ICollection: " + E); } try { m_pCollectionToTest.CopyTo(badCastValueTypeArray, 0); m_test.Eval(false, "Err_292haied Expected ArrayTypeMismatchException when attempting to copy to value type array that cannot be cast to from ICollection."); } #if WINCORESYS catch (System.InvalidCastException) { } #endif catch (ArgumentException) { } catch (Exception E) { m_test.Eval(false, "Unknown Exception when attempting to copy to array that cannot be cast to from ICollection: " + E); } try { m_pCollectionToTest.CopyTo(arrayToCopyTo, 0); if (arrayToCopyTo.Length == expectedCopyTo.Length) { if (expectCopyToItemsOutOfOrder) { m_test.Eval(VerifyItemsOutOfOrder(expectedCopyTo, arrayToCopyTo, 0), "Err_70928ahpg Expected items and actual item differ"); } else { m_test.Eval(VerifyItemsInOrder(expectedCopyTo, arrayToCopyTo, 0), "Err_5688pqygb Expected items and actual item differ"); } } else { m_test.Eval(false, "Expected copied array length of " + expectedCopyTo.Length + ", but found a length of " + arrayToCopyTo.Length); } } catch (Exception E) { m_test.Eval(false, "Err_550578oqpg Unknown Exception when attempting to copy to array without enough room between index and end of array from ICollection: " + E); } try { m_pCollectionToTest.CopyTo(tArrayToCopyTo, 0); if (tArrayToCopyTo.Length == expectedCopyTo.Length) { if (expectCopyToItemsOutOfOrder) { m_test.Eval(VerifyItemsOutOfOrderT(expectedCopyTo, tArrayToCopyTo, 0), "Err_336879pqicbx Expected items and actual item differ"); } else { m_test.Eval(VerifyItemsInOrderT(expectedCopyTo, tArrayToCopyTo, 0), "Err_35488qpag Expected items and actual item differ"); } } else { m_test.Eval(false, "Expected copied array length of " + expectedCopyTo.Length + ", but found a length of " + tArrayToCopyTo.Length); } } catch (Exception E) { m_test.Eval(false, "Err_7839hpqg Unknown Exception when attempting to copy to T[] using CopyTo from ICollection: " + E); } //[] Non zero index try { tArrayToCopyTo = new T[expectedCopyTo.Length + 1]; m_pCollectionToTest.CopyTo(tArrayToCopyTo, 1); if (expectCopyToItemsOutOfOrder) { m_test.Eval(VerifyItemsOutOfOrderT(expectedCopyTo, tArrayToCopyTo, 1), "Err_56888apahpg Expected items and actual item differ"); } else { m_test.Eval(VerifyItemsInOrderT(expectedCopyTo, tArrayToCopyTo, 1), "Err_00289aogs Expected items and actual item differ"); } } catch (Exception E) { m_test.Eval(false, "Err_78928pqyb Unknown Exception when attempting to copy to T[] using CopyTo from ICollection: " + E); } if (copyToOnlySupportsZeroLowerBounds) { //[] Non zero lower bounds try { Array tempArray = Array.CreateInstance(typeof(Object), new int[] { expectedCopyTo.Length + 8 }, new int[] { -4 }); m_pCollectionToTest.CopyTo(tempArray, 0); m_test.Eval(false, "Expected Argument when attempting to copy to array that has a non zero lower bound"); } catch (ArgumentException) { } catch (PlatformNotSupportedException) { } catch (Exception E) { m_test.Eval(false, "Unknown Exception when attempting to copy to array that has a non zero lower bound: " + E); } } }
protected override void OnSetUp() { expectedInput = new MyValueType(1, 2, 3); expectedInputIndex = new MyValueType(12, 12, 12); expectedOutput = new MyValueType(3, 2, 1); }
public static void Run() { Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess().Id); Console.WriteLine("Press smth to run"); Console.ReadLine(); RunWithBenchmark($"WarmUp", () => {}, 1000); int stackAllocationCount = 1024 * 1024 / (4 + 8); stackAllocationCount += 87243; //it is the max number possible to insert in stack RunWithBenchmark($"Stack allocation {stackAllocationCount}", () => { RecursiveAction(new MyValueType { counter = stackAllocationCount }); }); RunWithBenchmark($"Array of {Count} ValueTypes", () => { var valueArray = new MyValueType[Count]; for (int i = 0; i < Count; i++) { var value = new MyValueType { counter = i, ticks = DateTime.Now.Ticks }; valueArray[i] = value; } }); RunWithBenchmark($"Array of {Count} ReferenceTypes", () => { var refArray = new MyRefType[Count]; for (int i = 0; i < Count; i++) { var value = new MyRefType { i = i, j = DateTime.Now.Ticks }; refArray[i] = value; } }); RunWithBenchmark($"List of {Count} ReferenceTypes", () => { var refList = new List <MyRefType>(Count); //w/o Count it allocates twice more (array grow) for (int i = 0; i < Count; i++) { var value = new MyRefType { i = i, j = DateTime.Now.Ticks }; refList.Add(value); } }); // =============================================== // THREADS // =============================================== var threadsCount = 10000; var threads = new Thread[threadsCount]; RunWithBenchmark($"Creating threads ({threadsCount})", () => { for (int i = 0; i < threadsCount; i++) { threads[i] = new Thread(CpuIntensiveJob); } }); RunWithBenchmark("Running threads", () => { for (int i = 0; i < threadsCount; i++) { threads[i].Start(i); } }); // =============================================== // TASKS // =============================================== var tasksCount = 10000; var tasks = new Task <double> [tasksCount]; RunWithBenchmark($"Creating tasks ({tasksCount})", () => { for (int i = 0; i < tasksCount; i++) { tasks[i] = new Task <double>(() => CpuIntensiveJob(i)); } }); RunWithBenchmark("Running tasks:", () => { for (int i = 0; i < tasksCount; i++) { tasks[i].Start(); } var taskResults = Task.WhenAll(tasks).GetAwaiter().GetResult(); }); Console.WriteLine("Press smth to finish"); Console.ReadLine(); // press key to start }
public static string setControlValueByValue(object Value, bool IsTransformed, MyValueType DataType) { string retval = ""; switch (DataType) { case MyValueType.Date: if (IsTransformed) { try { retval = DateTime.Parse(Value.ToString()).ToString("MM/dd/yyyy"); } catch { retval = ""; } } else { try { retval = Value.ToString(); } catch { retval = ""; } } break; case MyValueType.DateTime: if (IsTransformed) { try { retval = DateTime.Parse(Value.ToString()).ToString("MM/dd/yyyy hh:mm:ss"); } catch { retval = ""; } } else { try { retval = Value.ToString(); } catch { retval = ""; } } break; case MyValueType.Int: if (IsTransformed) { try { Int32 _Value = Int32.Parse(Value.ToString()); if (_Value <= 0) { retval = ""; } else { retval = _Value.ToString(); } } catch { retval = ""; } } else { try { Int32 _Value = Int32.Parse(Value.ToString()); retval = _Value.ToString(); } catch { retval = ""; } } break; case MyValueType.Decimal: if (IsTransformed) { try { decimal _Value = decimal.Parse(Value.ToString()); if (_Value <= 0) { retval = ""; } else { retval = _Value.ToString(); } } catch { retval = ""; } } else { try { decimal _Value = decimal.Parse(Value.ToString()); retval = _Value.ToString(); } catch { retval = ""; } } break; case MyValueType.String: try { retval = Value.ToString(); } catch { retval = ""; } break; } return(retval); }
public abstract void M(MyValueType v);