-2

Can we say var keyword is Compile time Polymorphism and Dynamic is Run Time Polymorphism?

var a=10; (Compile time Polymorphism)
dynamic a=10; (Dynamic is Run Time Polymorphism)
a="XYZ"; (Dynamic is Run Time Polymorphism)
Sukhraj
  • 75
  • 9
  • 1
    What "Yes" or "No" answer will give you? – SᴇM Feb 26 '20 at 12:18
  • 2
    `var` is simply shorthand for writing the type out explicitly. It has nothing to do with polymorphism. – Matthew Watson Feb 26 '20 at 12:18
  • None of those two keywords is in any way related to polymorhism. Of course you can use both keywords in the context of polymorhism, but you don´t have to. – MakePeaceGreatAgain Feb 26 '20 at 12:21
  • I know the concept of Polymorphism, but my question here is different, suppose someone asking you, is Var and Dynamic are polymorphism like in Polymorphism CLR decide at compile time (Compile Time Polymorphism) and run time (for overriding)the same concept exist here, to decide at compile and run time. – Sukhraj Feb 26 '20 at 12:37

2 Answers2

1

I don't think you can link var and dynamic with polymorphism exactly. Polymorphism is about actions and behaviours and not data type or bindings.

var is evaluated at compile-time and dynamic is evaluated at runtime. You are correct there though.

for a better understanding look into What's the difference between dynamic (C# 4) and var?

Also, a suggestion. A little bit of reading and searching would have answered your query.

More Description Polymorphism is about an entity that can take many forms or behave differently. As an object can be a parent class instance or a child class instance. Now var cannot take multiple types on its own. Its type is just inferred with the type of object being assigned to it. So technically var is not compile-time polymorphism. Rather it depends on the object being assigned to that. So, var and dynamic might help you achieve or demonstrate polymorphism, but they do not themselves are examples of polymorphism.

Shahzad
  • 1,677
  • 1
  • 12
  • 25
  • `dynamic` is *definitely* a way of achieving polymorphism. `var`, not so much. – Konrad Rudolph Feb 26 '20 at 12:19
  • 1
    @KonradRudolph Yes, I agree with the point. What I meant to say was, comparing var and dynamic does not seem to be a classic example of how someone should see and understand runtime and compile-time polymorphism. – Shahzad Feb 26 '20 at 12:20
  • I know the concept of Polymorphism, but my question here is different, suppose someone asking you, is Var and Dynamic are polymorphism like in Polymorphism CLR decide at compile time (Compile Time Polymorphism) and run time (for overriding)the same concept exist here, to decide at compile and run time. – Sukhraj Feb 26 '20 at 12:36
  • @Sukhraj See my edit – Shahzad Feb 26 '20 at 12:42
0

var and dynamic have no relation whatsoever to polymorphism. Polymorphism means that you can provide an instance of a more-derived type when a less-derived type is required. var and dynamic are just syntactic sugar; the compiler does the work of figuring out what type you mean or need given the situation (in the case of dynamic). Apples and oranges.

rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • 1
    “syntactic sugar” ≠ “not polymorphism”. The two are orthogonal. As I mentioned in another comment, `dynamic` is *definitely* a way of achieving polymorphism. – Konrad Rudolph Feb 26 '20 at 12:35