Mastering C# Variables: When to Use dynamic, var, or Specific Types for Optimal Performance

Rafael Araujo de Lima
3 min readOct 30, 2024

--

Source: microsoft.com

During a project, we need to declare various variables to handle data within the application. It is important to know the data type to avoid incorrect assignments or comparisons between incompatible data types. In C#, this can be done using three types of declarations: dynamic, var, or specific types (primitives or classes, for example).

dynamic

A dynamic variable is resolved at runtime, meaning the compiler does not check its type at compile time. Thus, any assignment to a dynamic type before execution will pass without any issues. The flexibility of runtime resolution means that a variable can be an integer at one point and then be reused as a string, for example.

dynamic value = 100;
value = "It’s a string";

In this code, this works because the variable type is determined during execution.

Advantages:

  • Allows greater flexibility with dynamic data, like JSON
  • Very useful between applications working with multiple languages or APIs with varied return types

Disadvantages:

  • Runtime errors may occur since the compiler does not check the type at compile time
  • Code maintenance and readability are more challenging because the type is not explicit
  • Performance is affected as type resolution happens at runtime, requiring memory allocation and deallocation according to assignments. In this case, variables are allocated on the heap, even if they are primitive types, resulting in performance loss due to heap reference, boxing and unboxing, and slower heap access compared to stack access.

var

var variables are resolved at compile time, meaning the compiler verifies assignments and validates them to prevent incorrect assignments. It detects the type based on the initialization expression, and after that, the type is fixed.

var number = 1; // defines type as int
number = "John Doe"; // compilation error (cannot assign string to an integer)

Advantages:

  • Less verbose, more generic code
  • Reduces redundancy, especially for complex types
  • Safety through type checking at compile time, reducing runtime errors
  • Better performance compared to dynamic, as memory is allocated according to the type

Disadvantages:

  • Less explicit compared to direct type declarations
  • May not be intuitive in complex environments

Specific Type

These can be either primitive types (int, bool, float, char, etc.) or reference types (string, classes, enums, etc.). These types are inferred at compile time and are the most secure to prevent incorrect assignments or manipulations.

int number = 200;

List<string> names = new List<string> {"John", "Mary"};

Car car = new Car();

Advantages:

  • Better readability and maintainability
  • Greater type safety and better runtime performance
  • When using Visual Studio, provides better support for IntelliSense and error checking

Disadvantages:

  • Less flexible, particularly in cases where the type is dynamic and only known at runtime
  • More verbose compared to var
Comparison Table

Conclusion

Choosing the correct type declaration is essential for performance and software development best practices. Considering factors such as performance, readability, maintainability, flexibility, and type safety, the choice of type should be careful and precise.

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Rafael Araujo de Lima
Rafael Araujo de Lima

Written by Rafael Araujo de Lima

A Senior Software Engineer with over 10 years of experience, specializing in software development with a focus on .NET Core, ASP.NET Core, C#.

No responses yet

Write a response