inheritance - How come C# can handle this obviously idiotic object promotion during run time? -


i c# language much. i'm playing around, , never use code below in production code. compiler fooled layout of struct. how come, string on super class can still written , read in run-time? have expected memory access violation. inspecting type during run time, says of type base, see noproblem() function execution. no super class has been instantiated.

how able function this?

using system; using system.runtime.interopservices;  namespace fiddle {     class program     {         static void main(string[] args)         {             var b = new base             {                 intonbase = 1             };             var overlay = new overlay();             overlay.base = b;             var super = overlay.super;             var intvalue = super.intonbase;             super.stringonsuper = "my test string";             var stringvalue = super.stringonsuper;             super.noproblem();             expressions.fiddle();         }     }      [structlayout(layoutkind.explicit)]     public struct overlay     {         [fieldoffset(0)]         public super super;         [fieldoffset(0)]         public base base;     }      public class super : base     {         public string stringonsuper { get; set; }          public void noproblem()         {             console.writeline("you know, " + this.gettype().name + " kind of class.");         }     }      public class base     {         public int intonbase { get; set; }     } } 

well told clr lay out memory in advance using structlayout (i should caveat saying based on learning today after experimenting , reading other answers suggested)

you can tell here clr not instantiating anything. throw npe. , can play around the constructor on super. isn't getting invoked, , won't.

basically directly accessing memory, , since string, int etc built in types safely interacting them. should require more "intention" user, , other commented questions pointing requiring unsafe declaration.

class program {     static void main(string[] args)     {         var b = new base         {             intonbase = 1         };         var overlay = new overlay();         overlay.base = b;         var super = overlay.super;         var intvalue = super.intonbase;         super.stringonsuper = 8;         var stringvalue = super.stringonsuper;         system.diagnostics.debug.writeline(stringvalue);         super.noproblem();     } }  [structlayout(layoutkind.explicit)] public struct overlay {     [fieldoffset(0)]     public super super;     [fieldoffset(0)]     public base base; } public class newclass {     public string cat { get; set; } } public class super : base {     private super imnull;     public super()     {        // imnull = new super();         system.diagnostics.debug.writeline("well initialized...");     }     public int stringonsuper { get; set; }      public void noproblem()     {         system.diagnostics.debug.write("you know, " + this.gettype().name + " kind of class. super " + imnull.tostring() );     } }  public class base {     public int intonbase { get; set; } } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -