static void ForEach() { var list = new List <MutValueType> { new MutValueType { val = 10 }, new MutValueType { val = 20 }, new MutValueType { val = 30 }, }; ForEach1(list); var array = new MutValueType[] { new MutValueType { val = 100 }, new MutValueType { val = 200 }, new MutValueType { val = 300 }, }; ForEachArray1(array); }
static void Using1() { Console.WriteLine("Using:"); using (var x = new MutValueType()) { x.Increment(); } }
static void Box() { object o = new MutValueType { val = 300 }; ((MutValueType)o).Increment(); ((MutValueType)o).Increment(); }
static void Using3() { Console.WriteLine("Using with variable declared outside:"); MutValueType z; using (z = new MutValueType()) { z.Increment(); } }
static void BoxToStringCalls() { Console.WriteLine("BoxToStringCalls:"); MutValueType m = new MutValueType { val = 400 }; Console.WriteLine(m.ToString()); Console.WriteLine(((object)m).ToString()); Console.WriteLine(m.ToString()); }
static void Using2() { Console.WriteLine("Not using:"); var y = new MutValueType(); try { y.Increment(); } finally { MutValueType x = y; x.Dispose(); } }
static void Field() { ReadonlyField.Increment(); ReadonlyField.Increment(); MutableField.Increment(); MutableField.Increment(); // Ensure that 'v' isn't incorrectly removed // as a compiler-generated temporary MutValueType v = MutableField; v.Increment(); Console.WriteLine("Final value in MutableField: " + MutableField.val); }
public static void Main() { MutValueType m = new MutValueType(); RefParameter(ref m); ValueParameter(m); Field(); Box(); var gvt = new GenericValueType <string>("Test"); gvt.Call(ref gvt); new ValueTypeCall().InstanceFieldTests(); }
static void Field() { ReadonlyField.Increment(); ReadonlyField.Increment(); MutableField.Increment(); MutableField.Increment(); // Ensure that 'v' isn't incorrectly removed // as a compiler-generated temporary MutValueType v = MutableField; v.Increment(); Console.WriteLine("Final value in MutableField: " + MutableField.val); // Read-only field copies cannot be inlined for static methods: MutValueType localCopy = ReadonlyField; RefParameter(ref localCopy); }
static void Box() { Console.WriteLine("Box"); object o = new MutValueType { val = 300 }; ((MutValueType)o).Increment(); ((MutValueType)o).Increment(); MutValueType unboxed1 = (MutValueType)o; unboxed1.Increment(); unboxed1.Increment(); ((MutValueType)o).Increment(); MutValueType unboxed2 = (MutValueType)o; unboxed2.val = 100; ((MutValueType)o).Dispose(); }
static void ValueParameter(MutValueType m) { m.Increment(); m.Increment(); }
static void RefParameter(ref MutValueType m) { m.Increment(); m.Increment(); }