Ejemplo n.º 1
0
        //2: method(s) returning a ref struct
        public MyRefStruct MethodWithRetRefStruct()
        {
            var retVal = new MyRefStruct();

            //...
            return(retVal);
        }
Ejemplo n.º 2
0
 public int this[MyRefStruct o]
 {
     get
     {
         Assert.Equal(0, o.MyInt); // should be default(T)
         return(42);
     }
     set
     {
         Assert.Equal(0, o.MyInt); // should be default(T)
     }
 }
        public Task TestMethod3()
        {
            var  s2 = new MyRefStruct();
            Task result;

            try
            {
            }
            finally
            {
                result = s2.DisposeAsync().AsTask();
            }

            return(result);
        }
Ejemplo n.º 4
0
        public static void Run()
        {
            Console.WriteLine("This demonstrates the use of ref structs.");

            var myStruct    = new MyStruct();
            var myRefStruct = new MyRefStruct()
            {
                AnotherRefStruct = new AnotherRefStruct
                {
                    MyProperty = 123
                }
            };

            object boxedStruct = (object)myStruct;
            // Boxing is not possible for ref structs!
            // object boxedRefStruct = (object)myRefStruct;

            // can be passed around and returned
            var result = DoSomethingWithMyRefStruct(myRefStruct);
        }
Ejemplo n.º 5
0
 public static void TakesOutToRefStructAsArg(out MyRefStruct o)
 {
     throw new XunitException("Should never be called.");
 }
Ejemplo n.º 6
0
 public static void TakesRefStructAsArgWithDefaultValue(MyRefStruct o = default)
 {
     Assert.Equal(0, o.MyInt); // should be default(T)
 }
Ejemplo n.º 7
0
 public static void TakesRefStructAsArg(MyRefStruct o)
 {
     Assert.Equal(0, o.MyInt); // should be default(T)
 }
Ejemplo n.º 8
0
        //Produces an error as you cannot expose a ref struct to the heap
        public void LookAtRefStruct()
        {
            var obj = new MyRefStruct();

            //Console.WriteLine(obj);
        }
 public void TestMethod1()
 {
     using var s1 = new MyRefStruct();
 }
Ejemplo n.º 10
0
        public static MyRefStruct DoSomethingWithMyRefStruct(MyRefStruct refStruct)
        {
            Console.WriteLine($"Content: {refStruct.AnotherRefStruct.MyProperty}");

            return(refStruct);
        }
Ejemplo n.º 11
0
 //3: local variable
 public void MethodWithRefStructLocalVar()
 {
     var variable = new MyRefStruct();
     //...
 }
Ejemplo n.º 12
0
        //What is allowed with a ref struct:

        //1: method parameter
        public void MethodWithRefStructParam(MyRefStruct myRefStruct)
        {
            //...
        }