0

I have the following classes:

class Polygon
{
    protected string name;
    protected float width, height;
    public Polygon(string theName, float theWidth, float theHeight)
    {
        name = theName;
        width = theWidth;
        height = theHeight;
    }
    public virtual float calArea()
    {
        return 0;
    }
}

class Rectangle : Polygon
{
    public Rectangle(String name, float width, float height) : base(name,width,height)
    {
    }
    public override float calArea()
    {
        return width * height;
    }
}

Main function1:

   static void Main(string[] args)
    {
        Rectangle rect1  = new Rectangle("Rect1", 3.0f, 4.0f);
        float Area = rect1.calArea()
    }

Main function2:

 static void Main(string[] args)
    {
        Polygon poly = new Rectangle("Rect1", 3.0f, 4.0f);
        float Area = poly.calArea()
    }

I understand that Main function 2 uses dynamic binding.
If I change the override keyword to new in calArea method of Rectangle class, then it is static binding. What about main function 1? Does it use static/dynamic binding?

SeeknInspYre
  • 360
  • 1
  • 3
  • 16

2 Answers2

2

I don't think that 'dynamic' binding is right word for this. You are talking about compile-time (static) and run-time binding. If no class is inherited from Rectangle - then in example 1 there is enough information for compiler to decide which method will be called, and it can do a compile-time (static) binding.

[EDIT]:

Apparently I was not right. I examined generated IL code for the Example 1, and Example 2 with 'new' instead of 'override', and code of Main funcion appears to be the same:

  IL_000f:  newobj     instance void Console.Program/Rectangle::.ctor(string,
                                                                          float32,
                                                                          float32)
  IL_0014:  stloc.0
  IL_0015:  ldloc.0
  IL_0016:  callvirt   instance float32 Console.Program/Polygon::calArea()

From this code we see that even for Example 1 - callArea method is called from Polygon class. So, on the phase of compiling to IL code there is no binding to exact method implementation.

SergeyS
  • 3,515
  • 18
  • 27
  • Your answer conflicts with @Saurabh. I still don't know who is correct. Any sources for your answer? – SeeknInspYre Mar 22 '13 at 11:54
  • Btw dynamic binding is the correct terminology. http://stackoverflow.com/questions/640974/what-is-the-difference-between-static-and-dynamic-binding – SeeknInspYre Mar 22 '13 at 11:57
  • Actually under link you have mentioned there is a very interesting comment of @supercat. – SergeyS Mar 22 '13 at 12:04
  • Idea about vtable to be statically populated (not changing at run-time) makes sense. Here again a doubt what exactly to be called 'dynamic binding'. That's why I said that I don't like this word for your examples. – SergeyS Mar 22 '13 at 12:12
  • So are you saying that even Main Function 2 is static binding? Since I am not using the dynamic keyword/reflection... In other words, static binding can be of two types, one that is resolved at runtime, and one that is resolved at compile time. Anyway, my question still stands...is Main Function 1 being resolved at runtime or compile time? – SeeknInspYre Mar 23 '13 at 19:24
  • Yes, the last your comment is what I thinking currently. And concerning Function 1, I examined generated IL code and I now in doubt. What we are calling compile-time? Compile from C# to IL or JIT compilation from IL to binary? – SergeyS Mar 23 '13 at 20:36
  • Actually, if the information is available to the compiler, why ignore it? The compiler can see the object is of the type "Rectangle", so it should bind it to the Rectangle method? – SeeknInspYre Mar 24 '13 at 08:31
  • I think the same, but it seems that for the some reason this was not optimized by compiler. IL code clearly shows that we are still callling virtual method. – SergeyS Mar 24 '13 at 08:34
0

Non Virtual Method are static bound that means its known at compile time that which method to invoke. By providing new keyword you are telling compiler that this is out virtual /override area .

Main function 1 without defining new keyword , AFAIK will use dynamic binding because it still in scope for Virtual override.

TalentTuner
  • 17,262
  • 5
  • 38
  • 63