Home
Blog
When to Use Implicit Types (Like Var)
Updated
Dot Net Perls

When to Use Implicit Types (Like Var)

Most programming languages these days have implicit types—this is where we can use var, let, or some other keyword in place of a type like string or int. This feature can come in handy when dealing with more verbose type names—like Dictionary or HashMap types particularly. But exactly when should a developer use these implicit types?

My opinion is:

Use implicit types preferentially in almost all places in code—definitely prefer them on long type names (as mentioned).
For short type names, like int, it is probably better to still use var or let as changing the type to an Int64 (or whatever) will not require you to change the var, but it will require you to change an int.

Basically, var and let cut down on how much code people (or AIs as the case may be today) have to read. And when you refactor a type to have a different name, references to it that are using var or let don't need to be changed, reducing your work load.

I think, in languages like C#, var is nearly always better. In Rust and Swift we have let and everything is implicitly-typed by default. In Go we can use var, or just the special operator that assigns a new variable. In C++ (not covered on this site) we have auto which has the same effect.

Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
An RSS feed is available for this blog.
Home
Changes
© 2007-2025 Sam Allen