//optional
 public MyStruct(string myName = "Default name", int myNumber = 42, bool myBoolean = true)
 {
     this.myType    = new MyRefType();
     this.myName    = myName;
     this.myNumber  = myNumber;
     this.myBoolean = myBoolean;
 }
Exemple #2
0
        public void CustomReferenceTypeArgTest()
        {
            MyRefType arg = new MyRefType()
            {
                x = 2
            };
            var sel = Selectors.Create(() => arg, i => Tuple.Create(10, i != null ? i.x : -1));

            Assert.AreSame(sel(), sel());
            Assert.AreEqual(2, sel().Item2);

            arg = new MyRefType()
            {
                x = 3
            };
            Assert.AreSame(sel(), sel());
            Assert.AreEqual(3, sel().Item2);
            var r1 = sel();

            arg = new MyRefType()
            {
                x = 3
            };
            Assert.AreSame(sel(), sel());
            Assert.AreNotSame(r1, sel());
            Assert.AreEqual(3, sel().Item2);

            arg = null;
            Assert.AreSame(sel(), sel());
            Assert.AreEqual(-1, sel().Item2);
        }
    static void Main()
    {
        var myRefType = new MyRefType();

        myRefType.MyInt = 0;
        var x = new whatever(myRefType);

        Console.WriteLine(x);
        myRefType.MyInt = 1;
        Console.WriteLine(x);
        Console.ReadKey();
    }
 public whatever(MyRefType myRefType)
 {
     this.variable = () => myRefType.MyInt.ToString();
 }
Exemple #5
0
        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 MyObject(int X, MyRefType Y)
 {
     this.X = X; this.Y = Y;
 }