wasAsked.

var vs dynamic

HCA Healthcare · Role not specified · technical

Published

Year not specified · C#

Community answers

Posted by wasAsked editorial collection

`var` is compile-time type inference. The variable still has one specific static type, and invalid member access is a compile error. `dynamic` postpones member binding until runtime; the compiler permits the call, but it can fail with `RuntimeBinderException`. Neither keyword means “no type.” ```csharp var customer = new Customer("Asha"); // statically Customer Console.WriteLine(customer.Name); // checked at compile time dynamic payload = GetLegacyComObject(); try { Console.WriteLine(payload.CustomerName); // resolved at runtime } catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex) { logger.LogWarning(ex, "Unexpected legacy payload shape"); } ``` Prefer `var` when the assigned type is obvious and it improves readability, especially for anonymous types and LINQ projections. Use `dynamic` only at genuinely dynamic boundaries such as COM, some scripting systems, or legacy libraries. At JSON boundaries, typed DTOs or explicit `JsonElement` access are usually safer. `var value = GetDynamic()` still becomes dynamic if the expression's compile-time type is dynamic.

Prince Yadav · · 0 likes

var and dynamic both let you avoid writing a type name, but they are fundamentally different. var — compile-time type inference - The compiler determines the type from the value assigned, and the variable keeps that type permanently. - Fully type-safe: errors are detected at compile time. - Gives full IntelliSense and normal performance (the compiled IL is the same as writing the explicit type). - Only for local variables, and must be initialised. var name = "Asha"; // string name = 42; // compile error: cannot convert int to string var items = new Dictionary<string, List<Order>>(); // saves repeating a long type dynamic — runtime binding - The compiler skips type checking; member access and operations are resolved at runtime. - The value can change type at any time. - No compile-time errors for mistakes; they become RuntimeBinderException at runtime. - Slower, because the runtime binder resolves calls (results are cached per call site). - Can be used anywhere a type is allowed: fields, parameters, return types. dynamic value = "Asha"; Console.WriteLine(value.Length); // 4 value = 42; Console.WriteLine(value.Length); // compiles, but throws at runtime Typical uses - var: everyday local variables, anonymous types from LINQ, long generic types, using and foreach declarations. - dynamic: COM interop with Office, ExpandoObject, interoperating with dynamic languages, or quick scripting-style code. Avoid it in normal business code, and prefer typed models or JsonNode for JSON. Common confusion - var is not like JavaScript's var; C# var is strongly typed. - dynamic is not the same as object: with object you must cast before calling specific members, and the compiler checks those calls; with dynamic the compiler does not check at all. Interview summary: "var = compiler infers a fixed type; dynamic = the type is checked only at runtime. Use var freely, use dynamic rarely and deliberately."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics