//Main_3_4_1 public static void Main() { MyStruct ms = new MyStruct(); MyClass mc = new MyClass(); Console.WriteLine("IL Keywords."); Console.Read(); }
static void Main() { var inst1 = new MyClass(); inst1.x = 10; ChangeMyClass(inst1); System.Console.WriteLine(inst1.x); var inst2 = new MyStruct(); inst2.x = 10; ChangeMyStruct(ref inst2); System.Console.WriteLine(inst2.x); var inst3 = inst1; inst1.x = 5; System.Console.WriteLine(inst3.x); var inst4 = inst2; inst2.x = 5; System.Console.WriteLine(inst4.x); var inst5 = new MyClass(); inst5.x = 5; System.Console.WriteLine(System.String.Format("inst3 and inst1 are {0}equal!", (inst3 == inst1) ? "" : "not ")); System.Console.WriteLine(System.String.Format("inst5 and inst1 are {0}equal!", (inst5 == inst1) ? "" : "not ")); var s1 = "Hello Trainer!"; System.String s2 = s1; s1 = "Hello Trainees!"; System.Console.WriteLine(s1); System.Console.WriteLine(s2); System.Console.WriteLine(System.String.Format("s1 and s2 are {0}equal!", (s1==s2) ? "" : "not ")); var s3 = System.Console.ReadLine(); System.Console.WriteLine(System.String.Format("s1 and s3 are {0}equal!", (s1==s3) ? "" : "not ")); }
public static void Main() { //使用缺省构造函数创建对象: MyStruct Location1 = new MyStruct(); MyClass Employee1 = new MyClass(); //显示值: Console.WriteLine("Default values:"); Console.WriteLine("\tStruct members: {0}, {1}", Location1.x, Location1.y); Console.WriteLine("\tClass members: {0}, {1}", Employee1.name, Employee1.id); //使用带有参数的构造函数创建对象: MyStruct Location2 = new MyStruct(10, 20); MyClass Employee2 = new MyClass(1234, "John Martin Smith"); //显示值: Console.WriteLine("Assigned values:"); Console.WriteLine("\tStruct members: {0}, {1}", Location2.x, Location2.y); Console.WriteLine("\tClass members: {0}, {1}", Employee2.name, Employee2.id); Console.ReadLine(); }
static void Main(string[] args) { Checker check = new Checker(); ClassA try1 = new ClassA(); ClassB try2 = new ClassB(); ClassC try3 = new ClassC(); ClassD try4 = new ClassD(); MyStruct try5 = new MyStruct(); object try6 = try5; Console.WriteLine("Analyzing ClassA type variable:"); // Анализ переменной типа ClassA check.Check(try1); Console.WriteLine("\nAnalyzing ClassB type variable:"); // Анализ переменной типа ClassB check.Check(try2); Console.WriteLine("\nAnalyzing ClassC type variable:"); // Анализ переменной типа ClassC check.Check(try3); Console.WriteLine("\nAnalyzing ClassD type variable:"); // Анализ переменной типа ClassD check.Check(try4); Console.WriteLine("\nAnalyzing MyStruct type variable:"); // Анализ переменной типа MyStruct check.Check(try5); Console.WriteLine("\nAnalyzing boxed MyStruct type variable:"); // Анализ упакованной check.Check(try6); Console.ReadKey(); }
static void TestConstrainedMethodCalls() { using (MyStruct s = new MyStruct()) { ((Object)s).ToString(); } }
// Caller method takes 5 parameters. 4 in register and 1 on the stack. The important details here are that // * Must take a value type as a parameter // * The value type must be of size 1, 2, 4 or 8 // * That parameter must be passed on the stack. Typically this is the 5th parameter and beyond for // static methods, or 4th and beyond for instance methods. static int Caller(int regParam1, int regParam2, int regParam3, int regParam4, MyStruct stackParam1) { // Add random calls to block inlining of this call into the parent frame. Console.Write("Let's "); Console.Write("Discourage "); Console.Write("Inlining "); Console.Write("Of "); Console.Write("Caller "); Console.Write("Into "); Console.WriteLine("Main."); // Avoid touching stackParam1 except to pass it off to the callee. Any non-trivial usage of // stackParam1 or other code within this method will likely eliminate the potential for tail // call optimization. // if (stackParam1.C == 9) Console.WriteLine("C == 9"); // Now make our call. // The keys here are: // * That the incoming value type stack parameter must be passed to the callee in register. // * Tail call optimizations must be enabled // * The tail call optimization must fire (see above for an example of what might block it). // The JIT will incorrectly load the outgoing parameter from the incorrect stack location // on the local frame. return Callee(stackParam1, regParam1, regParam2, regParam3, regParam4); }
public static int DoSomething(string parameter1, string parameter2, ref MyStruct myStruct) { int result; MessageBox.Show(parameter1); MessageBox.Show(parameter2); try { /* myStruct.X = 5; myStruct.Y = 6.5; myStruct.Z = 25; myStruct.FirstName = "İsmail"; myStruct.LastName = "Kocacan"; myStruct.Array[0] = "value1"; myStruct.Array[1] = "value2"; myStruct.Array[2] = "value3"; */ myStruct.KeyValue[0].Key = "x key 1"; myStruct.KeyValue[0].Value = "x value 1"; MessageBox.Show("KeyValue : " + myStruct.KeyValue[0].Key); result = 1; } catch (Exception e) { MessageBox.Show(e.Message, "Error"); result = 0; } return result; }
static void Main(string[] args) { List<MyStruct> my_list = new List<MyStruct>(); for (int currEntry = 0; currEntry < 100000; currEntry++) { MyStruct mys = new MyStruct(); mys.my_s2.my_long = 999; mys.my_int = 33; mys.my_ary = new byte[] { 4, 5, 6, 7 }; my_list.Add(mys); } MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); DateTime startTime = DateTime.Now; bf.Serialize(ms, my_list); ms.Position = 0; List<MyStruct> my_list2 = (List<MyStruct>)bf.Deserialize(ms); DateTime endTime = DateTime.Now; TimeSpan ts = new TimeSpan(endTime.Ticks - startTime.Ticks); Console.WriteLine("Serialize/Deserialize span:" + ts.Milliseconds); }
public static void Main() { MyStruct newObj = new MyStruct(),noNewObj; System.Console.WriteLine(newObj.x); //System.Console.WriteLine(noNewObj.x);thats thorws an error newObj.x=noNewObj.x=20; System.Console.WriteLine(newObj.x + "\n" + noNewObj.x); }
public void Run() { MyStruct s = new MyStruct(); s.b = "OK"; MyDelegate dg = new MyDelegate(s.Test); dg(); }
public static bool IsXGeater(MyStruct line) { if (line.v1.val > line.v2.val) { line = new MyStruct(line.v2, line.v1); return true; } return false; }
public static void UsingExample() { var obj = new MyStruct(); obj = new MyStruct(false); obj.Flag = true; obj.SValue = "Hello, World!"; MyStruct.IValue = 10; }
private static int SumMSH(MyStruct[] ms) { int sum = 0; for (int i = 0; i < ms.Length; i++) { sum += ms[i].h; //Gives an AV //sum += ms[i].b; //will not give an AV since offset is less than 8 bytes. } return sum; }
public void Test_Add() { var svd = new SaveDataContainer (); var key = "Item0"; var value = new MyStruct (1, 2f, "3"); svd.Add (key, value); Assert.AreEqual (1, svd.ItemCount); Assert.AreEqual (1, svd.Items.Count ()); }
public void Run() { MyStruct s = new MyStruct(); TestApi.WriteLine(s.GetType().ToString()); TestApi.WriteLine(s.ToString()); MyEnum e = MyEnum.Default; //TestApi.WriteLine(e.CompareTo(e).ToString()); TestApi.WriteLine(e.GetTypeCode().ToString()); TestApi.WriteLine(e.ToString()); }
static void Main() { var i_fuenf = 5; PrintType(i_fuenf); var inst1 = new MyClass(); PrintType(inst1); var inst2 = new MyStruct(); PrintType(inst2); }
//Create an archive of items to add custom data for them void FillArchive(){ MyStruct temp = new MyStruct(); // Item 1 CustomItemCreator (temp, "item_test", "usar", "dar", "comentar", "mirar"); myStructList.Add (temp); temp = new MyStruct(); //Item 2 CustomItemCreator(temp, "item_test2", "usar", "dar", "comentar"); myStructList.Add (temp); }
public void Test() { var struct1 = new MyStruct {Age = 10, Name = "1234"}; var struct2 = new MyStruct { Age = 10, Name = "1234" }; Assert.AreEqual(struct1, struct2); var class1 = new MyClass { Age = 10, Name = "1234" }; var class2 = new MyClass { Age = 10, Name = "1234" }; Assert.AreNotEqual(class1, class2); }
public virtual MyStruct Add (int a, out int c, int b) { Console.WriteLine ("ADD"); c = a + b; MyStruct res = new MyStruct (); res.a = a; res.b = b; res.c = c; return res; }
public void Test_GetOrCreate() { var svd = new SaveDataContainer (); var key = "Item0"; var value = new MyStruct (1, 2f, "3"); svd.GetOrCreate (key, value); Assert.AreEqual (1, svd.ItemCount); Assert.AreEqual (value, svd.Get (key)); Assert.AreEqual (value, svd[key]); }
public StoreStruct() { myStruct = new MyStruct(); myStruct.anInt = 9999; myStruct.aString = "string in a struct"; myStruct.aBool = true; myStruct.insideStruct = new MyStructInside(); myStruct.insideStruct.anInt = 88888; myStruct.insideStruct.aString = "string in a struct in a struct"; myStruct.insideStruct.boolList = new VelocityDbList<bool>(2); myStruct.intList = new List<int>(); in16 = 16; }
//Use this for initialization void Start() { MyClass mClass = new MyClass(); MyStruct mStruct = new MyStruct(); mClass.a = 1; mStruct.a = 1; MyStruct ms = mStruct; ms.a = 3; Debug.Log(ms.a + " and " + mStruct.a); MyClass mc = mClass; mc.a = 3; Debug.Log(mc.a + " and " + mClass.a); }
// Callee method takes 5 parameters. 4 in register and 1 on the stack. The important details here are that // * Must take a value type as a parameter // * The value type must be of size 1, 2, 4 or 8 // * That parameter must be passed in register. Typically this is the 4th parameter or before for // static methods, or 3rd or before for instance methods. static int Callee(MyStruct regParam1, int regParam2, int regParam3, int regParam4, int stackParam1) { // If all conditions are met, Callee enters with an incorrect value for regParam1 // This should print "9 0 1 2 3". If the tail call is made incorrectly, // the result is (typically) "418858424 0 1 2 3". System.Console.WriteLine("Printing Outputs: {0} {1} {2} {3} {4}", regParam1.C, regParam2, regParam3, regParam4, stackParam1); return regParam1.C; }
public static int Main() { MyStruct? ms = new MyStruct (); int v; v = ms == "a"; if (v != 1) return 1; v = "b" != ms; if (v != -2) return 2; return 0; }
//Helper for FillArchive (adds the custom data we where talking about) void CustomItemCreator(MyStruct myStruct, string _id, string _action1, string _action2=null, string _action3=null, string _action4=null){ myStruct.id = _id; myStruct.actionList.Add (_action1); if (_action2 != null) { myStruct.actionList.Add (_action2); } if (_action3 != null) { myStruct.actionList.Add (_action3); } if (_action4 != null) { myStruct.actionList.Add (_action4); } }
private static MyStruct[] InitMS(int length) { MyStruct[] ms = new MyStruct[length]; for (int i = 0, j = 0; i < ms.Length; i++) { ms[i].a = j++; ms[i].b = j++; ms[i].c = j++; ms[i].d = j++; ms[i].e = j++; ms[i].f = j++; ms[i].g = j++; ms[i].h = j++; } return ms; }
public void Create_AsIDictionary() { var myStruct = new MyStruct { A = 21, B = "Pu <3 Pi", C = 3.14M }; var dict = ObjectAccessor.Create(myStruct) as IDictionary<string, object>; Assert.AreEqual(3, dict.Count); Assert.IsTrue(dict.Keys.Contains(nameof(MyStruct.A)) && dict.Keys.Contains(nameof(MyStruct.B)) && dict.Keys.Contains(nameof(MyStruct.C))); Assert.AreEqual(myStruct.A, dict[nameof(MyStruct.A)]); Assert.AreEqual(myStruct.B, dict[nameof(MyStruct.B)]); Assert.AreEqual(myStruct.C, dict[nameof(MyStruct.C)]); }
public static void Main() { MyStruct st = new MyStruct(); Console.WriteLine("Enter Car name true or false"); st.Car = bool.Parse(Console.ReadLine()); if(st.Car) { Console.WriteLine("Enter Car Name"); st.CarName = Console.ReadLine(); Console.WriteLine("Enter Car model"); st.CarModel = Console.ReadLine(); Console.WriteLine("Car name {0} ", st.CarName); Console.WriteLine("Car Model {0} " , st.CarModel); } }
private int Run() { MyStruct m = new MyStruct(); m.myInt = Int32.MinValue; m.myStr = "This is the string"; m.myLong = Int64.MaxValue; m.myDouble = Double.MinValue; m.myMutex = new Mutex(true); Thread t = new Thread(new ParameterizedThreadStart(ThreadWorker)); t.Start(m); // Test to see if it passing an owned mutex Thread.Sleep(1000); m.myMutex.ReleaseMutex(); t.Join(); Console.WriteLine(100 == iRet ? "Test Passed" : "Test Failed"); return (100 == iRet ? 100 : -1); }
public MeshOutline Init() { if (inited ) return this; inited = true; mf = GetComponentInChildren<MeshFilter>(); if (mf == null) return this; var m = mf.sharedMesh; vert = mf.sharedMesh.vertices; if (!res.outlines.TryGetValue(name, out outline)) { res.outlines[name] = outline = new List<int>(); //res.outlineDict.Add(new OutlineDict() { Name = name, outlineValues = outline }); var cnt = new Dictionary<Vector3, MyStruct>(); for (int i = 0; i < m.triangles.Length; i++) { var vector3 = m.vertices[m.triangles[i]]; MyStruct myStruct; if (!cnt.TryGetValue(vector3, out myStruct)) myStruct = cnt[vector3] = new MyStruct(); myStruct.id.Add(m.triangles[i]); myStruct.cnt++; } foreach (var a in cnt) if (a.Value.cnt < 4) outline.AddRange(a.Value.id); } meshFilter = new MeshFilter2() { m = mf, verts = mf.sharedMesh.vertices }; foreach (int b in this.outline) { if (meshFilter.verts.Length <= b) continue; meshFilter.myVerts.Add(new MyVert() { m = meshFilter, i = b }); } return this; }
public void Main() { MyStruct a; MyStruct b = new MyStruct(10); }
public static void StructRef(ref MyStruct value) { value.x++; value.y++; }
public void Function9(MyStruct param) { Console.WriteLine("structArg"); }
public void CanCreateStructInstance() { MyStruct m = ReflectionTools.CreateInstance <MyStruct>(); }
static void Main(string[] args) { // Int测试说明:ref 参数可以使 值类型具有引用类型的特性 // 测试1 int x = 0; IntRef(x); Console.WriteLine(x); // 测试2 int y = 0; IntRef(ref y); Console.WriteLine(y); // Struct测试说明:ref 参数可以 使结构体具有引用类型的特性 MyStruct ms1 = new MyStruct(0, 0); StructRef(ms1); Console.WriteLine("x: " + ms1.x + "y: " + ms1.y); MyStruct ms2 = new MyStruct(0, 0); StructRef(ref ms2); Console.WriteLine("x: " + ms2.x + "y: " + ms2.y); // Class第一种测试说明: 有无 ref 参数,对于引用类型,效果是一样的 MyClass mc1 = new MyClass(0); ClassRef(mc1); Console.WriteLine(mc1.x); MyClass mc2 = new MyClass(0); ClassRef(ref mc2); Console.WriteLine(mc2.x); // Calss第二种测试说明: // 结合第一种测试可知,引用类型可以修改引用指向的内存数据 // 但是,无法修改方法外的引用类型原来的引用 // 此时,可以通过 ref 参数来实现,这是 ref 第二种作用 List <int> intList1 = new List <int>(); intList1.Add(0); Class2Ref(intList1); Console.WriteLine("list1: " + intList1[0]); List <int> intList2 = new List <int>(); intList2.Add(0); Class2Ref(ref intList2); Console.WriteLine("list2: " + intList2[0]); Console.WriteLine("----------------"); Dictionary <int, int> _idct = new Dictionary <int, int>(); _idct.Add(0, 0); _idct[0]++; Console.WriteLine(_idct[0]); Console.WriteLine("----------------"); List <byte> byelist = new List <byte>(); byelist.Add(10); byelist.Add(11); byelist.Add(12); Class2Ref(byelist); Console.WriteLine(byelist[0]); Console.WriteLine("-----------"); int ap = 10; Class2Ref(ap); Console.WriteLine(ap); Class2Ref(ref ap); Console.WriteLine(ap); Console.WriteLine("---------------"); int[] ss = new int[] { 1, 2, 3 }; Class2Ref(ss); Console.WriteLine(ss[0]); Class2Ref(ref ss); Console.WriteLine(ss[0]); Console.ReadLine(); }
public void ReferenceEquals_WithSameStructs_ReturnsTrue() { var s = new MyStruct(); Assert.Equal(true, Object.ReferenceEquals(s, s)); }
static void TestConstrainedMethodCalls_Unsupported() { MyStruct s = new MyStruct(); s.ToString(); }
static bool NegBla(ref MyStruct my) => !my.Bla;
// Parameter is a value type (a copy) static MyStruct NegateStruct(MyStruct s) { MyStruct res = new MyStruct(-1 * s.a, -1 * s.b); return(res); }
public virtual void MyMethodWithStruct(ref MyStruct s) { s.Value = 2 * s.Value; }
public static void StructTest1() { var m = new MyStruct[10]; m[1] = new MyStruct(); // `Throw exception here.` }
private static void ValueTypeMethod(MyStruct s) { Assert.Equal(s.X, 0); Assert.Equal(s.Y, 0); }
private static void RefValueTypeMethod(ref MyStruct s) { s.X += 7; s.Y += 8; }
public static int MyStructGetHashCode(MyStruct s) { return(143); }
public static string MyStructToString(MyStruct s) { return(s.Value); }
public unsafe static void Ldflda(MyStruct value) { int *ptr = &value.Foo; Console.WriteLine(*ptr); }
private static void TestStep1() { //var xItem = xQueue.Dequeue(); //Console.Write("Char: "); //Console.WriteLine(xResult.KeyChar); var xItem = new MyStruct { A = 1, B = 2, C = 3, D = 4, E = 5 }; var xArray = new MyStruct[1]; xArray[0] = xItem; //xArray[0] = new MyStruct(1, 2, 3, 4, 5); xItem = xArray[0]; Assert.IsTrue(xItem.A == 1, "xItem.A == 1"); Console.Write("A: "); Console.WriteLine(xItem.A); Assert.IsTrue(xItem.B == 2, "xItem.B == 2"); Console.Write("B: "); Console.WriteLine(xItem.B); Assert.IsTrue(xItem.C == 3, "xItem.C == 3"); Console.Write("C: "); Console.WriteLine(xItem.C); Assert.IsTrue(xItem.D == 4, "xItem.D == 4"); Console.Write("D: "); Console.WriteLine(xItem.D); Assert.IsTrue(xItem.E == 5, "xItem.E == 5"); Console.Write("E: "); Console.WriteLine(xItem.E); //xItem = new MyStruct(6, 7, 8, 9, 10); Console.WriteLine("Next: "); //xItem = xQueue.Dequeue(); //Console.Write("Char: "); //Console.WriteLine(xResult.KeyChar); //var xArray = new MyStruct[0]; //xArray[0] = new MyStruct(1, 2, 3, 4, 5); var xItem2 = GetValue(xArray, 0); Assert.IsTrue(xItem2.A == 1, "xItem2.A == 1"); Console.Write("A: "); Console.WriteLine(xItem2.A); Assert.IsTrue(xItem2.B == 2, "xItem2.B == 2"); Console.Write("B: "); Console.WriteLine(xItem2.B); Assert.IsTrue(xItem2.C == 3, "xItem2.C == 3"); Console.Write("C: "); Console.WriteLine(xItem2.C); Assert.IsTrue(xItem2.D == 4, "xItem2.D == 4"); Console.Write("D: "); Console.WriteLine(xItem2.D); Assert.IsTrue(xItem2.E == 5, "xItem2.E == 5"); Console.Write("E: "); Console.WriteLine(xItem2.E); }
internal static ITestPrx Run(TestHelper helper) { Communicator communicator = helper.Communicator !; var test = ITestPrx.Parse(helper.GetTestProxy("test", 0), communicator); TextWriter output = helper.Output; output.Write("testing BitSequence and ReadOnlyBitSequence... "); Span <byte> span1 = stackalloc byte[7]; Span <byte> span2 = stackalloc byte[3]; var bitSequence = new BitSequence(span1, span2); TestHelper.Assert(bitSequence.Length == 80); var onBits = new int[] { 0, 9, 35, 69, 70, 71, 79 }; foreach (int i in onBits) { bitSequence[i] = true; } bitSequence[69] = true; // double true for (int i = 0; i < bitSequence.Length; ++i) { TestHelper.Assert(bitSequence[i] == onBits.Contains(i)); bitSequence[i] = !bitSequence[i]; } for (int i = 0; i < bitSequence.Length; ++i) { TestHelper.Assert(bitSequence[i] != onBits.Contains(i)); bitSequence[i] = !bitSequence[i]; // back to original value } try { bitSequence[81] = true; TestHelper.Assert(false); } catch (IndexOutOfRangeException) { // expected } try { bitSequence[-5] = true; TestHelper.Assert(false); } catch (IndexOutOfRangeException) { // expected } Span <byte> span = stackalloc byte[10]; span1.CopyTo(span); span2.CopyTo(span.Slice(7)); var roBitSequence = new ReadOnlyBitSequence(span); TestHelper.Assert(roBitSequence.Length == 80); for (int i = 0; i < roBitSequence.Length; ++i) { TestHelper.Assert(roBitSequence[i] == onBits.Contains(i)); } try { bool _ = roBitSequence[80]; TestHelper.Assert(false); } catch (IndexOutOfRangeException) { // expected } try { bool _ = roBitSequence[-5]; TestHelper.Assert(false); } catch (IndexOutOfRangeException) { // expected } output.Flush(); output.WriteLine("ok"); output.Write("testing basic operations with optional parameters... "); test.OpInt(null); test.OpInt(test.OpReturnInt()); test.OpString(null); test.OpString(test.OpReturnString()); test.OpBasic(17, 17, "test", "test"); test.OpBasic(17, 17, null, "test"); test.OpBasic(17, null, null, "test"); (int?r, int o1, int?o2, string?o3) = test.OpBasicReturnTuple(5, 15, "test"); TestHelper.Assert(r !.Value == 15 && o1 == 5 && o2 !.Value == 15 && o3 ! == "test"); (r, o1, o2, o3) = test.OpBasicReturnTuple(6, null, null); TestHelper.Assert(r == null && o1 == 6 && o2 == null && o3 == null); output.WriteLine("ok"); output.Write("testing operations with proxies and class parameters... "); TestHelper.Assert(test.OpObject(test, test) !.Equals(test)); TestHelper.Assert(test.OpObject(test, null) == null); TestHelper.Assert(test.OpTest(test, test) !.Equals(test)); TestHelper.Assert(test.OpTest(test, null) == null); var classInstance = new C(42); AnyClass?anyClass = test.OpAnyClass(classInstance, classInstance); TestHelper.Assert(anyClass != null && ((C)anyClass).X == 42); TestHelper.Assert(test.OpAnyClass(classInstance, null) == null); TestHelper.Assert(test.OpC(classInstance, classInstance) !.X == 42); TestHelper.Assert(test.OpC(classInstance, null) == null); try { test.OpObject(null !, null); TestHelper.Assert(false); } catch (NullReferenceException) { } try { test.OpTest(null !, null); TestHelper.Assert(false); } catch (NullReferenceException) { } // We detect null class instances through asserts during marshaling. output.WriteLine("ok"); output.Write("testing operations with sequence<T?> parameters... "); int?[] intSeq = new int?[] { 1, -5, null, 19, -35000 }; TestHelper.Assert(test.OpOptIntSeq(intSeq).SequenceEqual(intSeq)); TestHelper.Assert(test.OpTaggedOptIntSeq(intSeq) !.SequenceEqual(intSeq)); TestHelper.Assert(test.OpTaggedOptIntSeq(null) == null); string?[] stringSeq = new string?[] { "foo", "test", null, "", "bar" }; TestHelper.Assert(test.OpOptStringSeq(stringSeq).SequenceEqual(stringSeq)); TestHelper.Assert(test.OpTaggedOptStringSeq(stringSeq) !.SequenceEqual(stringSeq)); TestHelper.Assert(test.OpTaggedOptStringSeq(null) == null); output.WriteLine("ok"); output.Write("testing operations with dictionary<K, V?> parameters... "); Dictionary <int, int?> intIntDict = new Dictionary <int, int?> { { 1, -5 }, { 3, null }, { 5, 19 }, { 7, -35000 } }; TestHelper.Assert(test.OpIntOptIntDict(intIntDict).DictionaryEquals(intIntDict)); TestHelper.Assert(test.OpTaggedIntOptIntDict(intIntDict) !.DictionaryEquals(intIntDict)); TestHelper.Assert(test.OpTaggedIntOptIntDict(null) == null); Dictionary <int, string?> intStringDict = new Dictionary <int, string?> { { 1, "foo" }, { 3, "test" }, { 5, null }, { 7, "bar" } }; TestHelper.Assert(test.OpIntOptStringDict(intStringDict).DictionaryEquals(intStringDict)); TestHelper.Assert(test.OpTaggedIntOptStringDict(intStringDict) !.DictionaryEquals(intStringDict)); TestHelper.Assert(test.OpTaggedIntOptStringDict(null) == null); output.WriteLine("ok"); output.Write("testing struct with optional data members... "); var myStruct = new MyStruct(test, null, new string?[] { "foo", null, "bar" }); MyStruct myStructResult = test.OpMyStruct(myStruct); TestHelper.Assert(myStruct != myStructResult); // the proxies and arrays can't be identical TestHelper.Assert(myStructResult.Proxy !.Equals(myStruct.Proxy) && myStructResult.X == myStruct.X && myStructResult.StringSeq !.SequenceEqual(myStruct.StringSeq !)); myStructResult = test.OpOptMyStruct(myStruct) !.Value; TestHelper.Assert(myStructResult.Proxy !.Equals(myStruct.Proxy) && myStructResult.X == myStruct.X && myStructResult.StringSeq !.SequenceEqual(myStruct.StringSeq !)); TestHelper.Assert(test.OpOptMyStruct(null) == null); output.WriteLine("ok"); output.Write("testing class with optional data members... "); var derived = new Derived(test, null, new string?[] { "foo", null, "bar" }, null, "test"); Derived derivedResult = test.OpDerived(derived); TestHelper.Assert(derivedResult.Proxy !.Equals(derived.Proxy) && derivedResult.X == derived.X && derivedResult.StringSeq !.SequenceEqual(derived.StringSeq !) && derivedResult.SomeClass == null && derivedResult.S == derived.S); derivedResult = test.OpOptDerived(derived) !; TestHelper.Assert(derivedResult.Proxy !.Equals(derived.Proxy) && derivedResult.X == derived.X && derivedResult.StringSeq !.SequenceEqual(derived.StringSeq) && derivedResult.SomeClass == null && derivedResult.S == derived.S); TestHelper.Assert(test.OpOptDerived(null) == null); output.WriteLine("ok"); output.Write("testing exception with optional data members... "); try { test.OpDerivedEx(); TestHelper.Assert(false); } catch (DerivedEx ex) { TestHelper.Assert(ex.Proxy == null && ex.X == 5 && ex.StringSeq !.SequenceEqual(new string?[] { "foo", null, "bar" }) && ex.SomeClass is C someClass && someClass.X == 42 && ex.S == "test"); } try { test.OpDerivedEx(context: new Dictionary <string, string> { { "all null", "yes" } }); TestHelper.Assert(false); } catch (DerivedEx ex) { TestHelper.Assert(ex.Proxy == null && ex.X == null && ex.StringSeq == null && ex.SomeClass == null && ex.S == null); } output.WriteLine("ok"); return(test); }
// Parameter is a reference type static void NegateStructInline(ref MyStruct s) { s.a *= -1; s.b *= -1; }
private static int GetInt32ArraySize(MyStruct str) { return(Marshal.SizeOf(str) / Marshal.SizeOf(typeof(Int32))); }
static void PassByRef(ref MyStruct myValue) { myValue.val = 42; }
public static void Box(MyStruct value) { Console.WriteLine(value); }
static void PassByValue(MyStruct myValue) { myValue.val = 50; }
static void fs(MyStruct s) { s.i++; s.s = "????"; s.f(); }
public void Method(MyStruct value) { Assert.Equal(CustomStructValue, value); }
// Вычислить значение к скобках static StructureList <MyStruct> DeterminateExpression(StructureList <MyStruct> list) { // Найдем 2 элемента с максимальным уровнем int max = GetMaxLvl(list); MyStruct element1 = new MyStruct(); element1.Lvl = -1; element1.PartOfString = ""; MyStruct element2 = new MyStruct(); element1.Lvl = -1; element1.PartOfString = ""; int num = 0; foreach (var element in list) { if (element.Lvl == max) { if (element1.Lvl == -1) { element1 = element; } else { element2 = element; break; } } num++; } // Номер операции (между двумя элементами) num--; string operation = list.Data[num].PartOfString; // Результат операции double res = 0; double value1 = Double.Parse(element1.PartOfString); double value2 = Double.Parse(element2.PartOfString); if (operation == "+") { res = value1 + value2; } if (operation == "-") { res = value1 - value2; } if (operation == "*") { res = value1 * value2; } if (operation == "/") { res = Math.Round(value1 / value2, 3); } list.Remove(num - 2); list.Remove(num - 2); list.Remove(num - 2); list.Remove(num - 2); list.Remove(num - 2); MyStruct newRes = new MyStruct { PartOfString = res.ToString(CultureInfo.InvariantCulture), Lvl = max - 1 }; list.Add(newRes, num - 2); return(list); }
public bool Equals(MyStruct other) { return(Number == other.Number && Equals(PO, other.PO) && Nested.Equals(other.Nested)); }
private static void ValueTypeMethod(MyStruct s) { Assert.Equal(0, s.X); Assert.Equal(0, s.Y); }
public int[][] ConvexHull(int[][] Kontur) { //function[CH] = convexhull2(Kontur) //CONVEXHULL Digunakan untuk mendapatkan convex hull dari suatu objek menggunakan metode 'Graham Scan'. //Masukan: Kontur = kontur objek, yamg berdimensi dua dengan kolom pertama berisi data Y dan kolom kedua berisi data X. //Keluaran: CH = Convex hull int jum = Kontur.Length; //Cari titik jangkar atau pivot int terkecil = 0; for (int a = 1; a < jum; a++) { if (Kontur[a][0] == Kontur[terkecil][0]) { if (Kontur[a][1] < Kontur[terkecil][1]) { terkecil = a; } } else if (Kontur[a][0] < Kontur[terkecil][0]) { terkecil = a; } } //Susun data dengan menyertakan sudut dan panjang, kecuali titik dengan posisi = terkecil int indeks = 0; List <MyStruct> Piksel = new List <MyStruct>(); for (int a = 0; a < jum; a++) { if (a == terkecil) { continue; } MyStruct ms = new MyStruct { y = Kontur[a][0], x = Kontur[a][1], jarak = Jarak(Kontur[terkecil], Kontur[a]), sudut = Sudut(Kontur[terkecil], Kontur[a]) }; Piksel.Insert(indeks, ms); indeks = indeks + 1; } Piksel.TrimExcess(); int jum_piksel = indeks; MyStruct x; //Lakukan pengurutan menurut sudut dan jarak for (int p = 1; p < jum_piksel; p++) { x = Piksel[p]; //Sisipkan x ke dalam data[1..p - 1] int q = p - 1; bool ketemu = false; while ((q >= 0) && (!ketemu)) { if (x.sudut < Piksel[q].sudut) { Piksel[q + 1] = Piksel[q]; q = q - 1; } else { ketemu = true; } Piksel[q + 1] = x; } } //Kalau ada sejumlah piksel dengan nilai sudut sama maka hanya yang jaraknya terbesar yang akan dipertahankan List <MyStruct> Piksel1 = Unik(Piksel); Piksel1.TrimExcess(); jum_piksel = Piksel1.Count; //Siapkan tumpukan List <MyStruct1> H = new List <MyStruct1>(); MyStruct1 titik = new MyStruct1(); //Proses pemindaian. Mula - mula sisipkan dua titik H.Add(new MyStruct1 { y = Kontur[terkecil][0], x = Kontur[terkecil][1] }); H.Add(new MyStruct1 { y = Piksel1[0].y, x = Piksel1[0].x }); MyStruct1 A = new MyStruct1(); MyStruct1 B = new MyStruct1(); int i = 1; int top = 1; while (i < jum_piksel) { titik.x = Piksel1[i].x; titik.y = Piksel1[i].y; //Ambil dua data pertama pada tumpukan H tanpa membuangnya A.x = H[top].x; A.y = H[top].y; B.x = H[top - 1].x; B.y = H[top - 1].y; //top = top + 1; bool putar = PutarKanan(A.x, A.y, B.x, B.y, titik.x, titik.y); if (putar == true) { //Pop data pada tumpukan H top = top - 1; } else { //Tumpuk titik ke tumpukan H top = top + 1; if (H.Count - 1 <= top) { H.Add(A); } H[top] = titik; i = i + 1; } } H.RemoveAt(H.Count - 1); H.TrimExcess(); //Ambil data dari tumpukan H List <MyStruct1> C = new List <MyStruct1>(); indeks = 0; while (top != -1) { //Pop data dari tumpukan H //C[indeks] = H[top]; C.Insert(indeks, H[top]); top = top - 1; //indeks = indeks + 1; } C.TrimExcess(); int[] xValue = C.Select(a => a.x).ToArray(); int[] yValue = C.Select(a => a.y).ToArray(); int[][] ConvexHull = new int[xValue.GetLength(0)][]; for (int t = 0; t < xValue.GetLength(0); t++) { ConvexHull[t] = new int[2] { yValue[t], xValue[t] }; } return(ConvexHull); }
public static void StructRef(MyStruct value) // 自定义的结构体 { value.x++; value.y++; }