Boxing struct object

image_pdfimage_print
   
  

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

struct MyPoint {
    public int x, y;
}

class Program {
    public static void UseThisObject(object o) {
        Console.WriteLine("Type of param: {0}", o.GetType());
        Console.WriteLine("Value of o is: {0}", o);
    }

    public static void BoxAndUnboxInts() {
        
        ArrayList myInts = new ArrayList();
        myInts.Add(88);
        myInts.Add(3);
        myInts.Add(9764);

        int firstItem = (int)myInts[0];
        Console.WriteLine("First item is {0}", firstItem);
    }

    public static void UseBoxedMyPoint(object o) {
        if (o is MyPoint) {
            MyPoint p = (MyPoint)o;
            Console.WriteLine("{0}, {1}", p.x, p.y);
        } else
            Console.WriteLine("You did not send a MyPoint.");
    }


    static void Main(string[] args) {
        int myInt = 99;

        UseThisObject(myInt);
        BoxAndUnboxInts();

        MyPoint p;
        p.x = 10;
        p.y = 20;
        UseBoxedMyPoint(p);
        UseBoxedMyPoint(1);
    }
}

   
     


This entry was posted in Data Types. Bookmark the permalink.