C# Polymorphism

后端 未结 4 1967
难免孤独
难免孤独 2020-12-25 10:19


What\'s the difference between run-time polymorphism and compile-time polymorphism? Also, what\'s the difference between early binding and late binding? Examples woul

4条回答
  •  天命终不由人
    2020-12-25 10:32

    UPDATE: Please see Eric Lippert’s comments to this answer.

    In C#2 all binding is early, because C#2 is a statically-typed language. A late binding language would be one in which the method binding occurs at run time. (C#4 includes a late binding feature with the introduction of dynamic.)

    I am not sure what you mean by run time vs. compile time polymorphism.

    The C# compiler will determine at compile time which method overload will be called. The run-time type of an instance will determine which implementation of a particular method overload will be executed. This is still considered early binding even though it happens at run time, because the selected method is constrained to be an implementation of a specific virtual method overload, and it is not possible for such a call to generate a type-related exception such as can occur with a dynamic language and late binding.

提交回复
热议问题