[RESOLVED] Compile-time vs. runtime types... I'm confused...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: [RESOLVED] Compile-time vs. runtime types... I'm confused...

  1. #1
    Join Date
    Apr 2008
    Posts
    61

    [RESOLVED] Compile-time vs. runtime types... I'm confused...

    Hi there fellow forum members.

    I wanted to call upon your knowledge, as I have a simple concept that I thought I understood until yesterday when I was looking at some code in Visual Studio 2008.

    The simple concept relates to reference types.

    Consider the following:

    Code:
        class Shape
        {
            public Shape() { }
    
            private string _petName = "Shape class";
    
            public string PetName
            {
                get { return this._petName; }
                set { this._petName = value; }
            }
        }
    
        class Circle : Shape
        {
            public Circle()
            {
                this.PetName = "Circle class";                        
            }
    
            public string circleItem = "";
        }
    
        class Square : Shape
        {
            public Square()
            {
                this.PetName = "Square class";
            }
    
            public string SquareItem = "";
        }
    
        class Triangle : Shape
        {
            public Triangle()
            {
                this.PetName = "Triangle class";
            }
    
            public string TriangleItem = "";
        }
    
        static class Program
        {
            [STAThread]
            static void Main()
            {
                List<Shape> Shapes = new List<Shape>();
                Shapes.Add(new Circle());
                Shapes.Add(new Square());
                Shapes.Add(new Triangle());
    
                foreach (Shape shape in Shapes)
                {
                    string objectType = shape.GetType().ToString();
                }
            }
        }
    In the foreach construct, and at compile-time, reference variable shape is of type Shape; however, at runtime, I understand that the foreach construct will assign objects that derive from Shape into the reference variable shape.

    My question is this: Is reference variable shape still of type Shape at runtime, even though it points to a object of a different type? Or does it change depending on the object it is pointing to? For example, in the first iteration of the foreach loop, would reference variable shape's type now be of type Circle?

    Again, sorry for the simplicity of the question, but clarification from you experienced developers would be well received.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Compile-time vs. runtime types... I'm confused...

    As far as I know you cannot create a list of a base class, but add deviated classes.
    It is like having an array of short int, but trying to apply long int.

    A list must be type consistent.

  3. #3
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Compile-time vs. runtime types... I'm confused...

    Quote Originally Posted by mshdsp
    My question is this: Is reference variable shape still of type Shape at runtime, even though it points to a object of a different type? Or does it change depending on the object it is pointing to? For example, in the first iteration of the foreach loop, would reference variable shape's type now be of type Circle?
    You have to understand the concept of inheritance in object oriented programming. The reference variable 'shape' can be of type 'Circle' which is a 'Shape'. For example in the world you insects. A mosquito is an insect. There are certain things that it shares with all insects. An ant is different from a mosquito but it is still an insect. So for example chemical manufacturers produce insect killers that can kill a variety of insects be they ants, roaches, mosquitos, etc.

    In your case the concrete type of the first instance in the list might be of type 'Circle' but it can be interpreted as a 'Shape' otherwise you would have had an error when tried to add it to the list.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Compile-time vs. runtime types... I'm confused...

    As far as I know you cannot create a list of a base class, but add deviated classes.
    It is like having an array of short int, but trying to apply long int.
    Yes you can. The reason why you can't add long ints to a list of short ints is because they are completely different unrelated types. long int does not inherit from short int.

    My question is this: Is reference variable shape still of type Shape at runtime,
    Yes, it is. A simple testcase is this:

    Code:
    public class BaseClass
    {
        public void Method ()
        {
            Console.WriteLine ("Base says hello");
        }
    }
    
    public class Derived : Base
    {
        public new void Method ()
        {
            Console.WriteLine ("Base says hello");
        }
    }
    Notice how the derived class declares a new method with the same name as the base class. Now, run this sample:

    Code:
    Base base = new Base ();
    base.Method ();
    
    Derived derived = new Derived ();
    derived.Method ();
    
    Base base = new Derived ();
    derived.Method ();
    As the method in question is *not* a virtual method, the compile time type is used to resolve which method will be invoked. Virtual methods will use the runtime type, i.e. the .NET framework will check the type of the object, then go to the virtual method table, then invoke the correct method (overridden) method.

    If the compiler was 'smart' and decided to change the type of the variable in that last example, you'd end up calling the incorrect method and the whole world would collapse!
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Compile-time vs. runtime types... I'm confused...

    Type of reference variable remains Shape, but the object which it references can be of different (derived) type. The referenced object can at runtime even be of type you have never imagined at compile time, because it can come from dynamicaly loaded (or created) assembly.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Compile-time vs. runtime types... I'm confused...

    Throw the screwball of 'dynamic' in c# 4/vs 10 and the universe implodes.

  7. #7
    Join Date
    Apr 2008
    Posts
    61

    Re: Compile-time vs. runtime types... I'm confused...

    Quote Originally Posted by boudino View Post
    Type of reference variable remains Shape, but the object which it references can be of different (derived) type. The referenced object can at runtime even be of type you have never imagined at compile time, because it can come from dynamicaly loaded (or created) assembly.
    That's what I thought. Thanks for the clarification.

  8. #8
    Join Date
    Apr 2008
    Posts
    61

    Re: Compile-time vs. runtime types... I'm confused...

    Thanks all for responding to this post; I am most grateful to you for taking the time to be so informative and polite.

  9. #9
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Compile-time vs. runtime types... I'm confused...

    Implodes? Explodes, doesn't it? It is just start of next level of spiral development of approaches. Back to basic but on another level from another direction whit slightly modified intention.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center