Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Reflection;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     [StructLayout(LayoutKind.Explicit)]
  8.     public struct CatOrDog
  9.     {
  10.         [FieldOffset(0)]
  11.         public Cat Cat;
  12.  
  13.         [FieldOffset(0)]
  14.         public Dog Dog;
  15.     }
  16.  
  17.  
  18.     class Program
  19.     {
  20.         static void Main(string[] args)
  21.         {
  22.             var catOrDog = new CatOrDog { Cat = new Cat { Name = "Jenny" } };
  23.             catOrDog.Dog.Say();
  24.         }
  25.     }
  26.  
  27.     public class Cat
  28.     {
  29.         public string Name { get; set; }
  30.  
  31.         public void Say()
  32.         {
  33.             Console.WriteLine("Meow " + Name);
  34.         }
  35.     }
  36.  
  37.     public class Dog
  38.     {
  39.         public string Name { get; set; }
  40.  
  41.         public void Say()
  42.         {
  43.             Console.WriteLine("Woof" + Name);
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement