Using Var - Implicitly Typed Fields - Quick Tip @WorldOfZeroDevelopment
Using Var - Implicitly Typed Fields - Quick Tip  @WorldOfZeroDevelopment
Uploaded February 2019 | Updated December 2026, 3 hours ago
C# is a statically typed language meaning you must specify what type your variables are when declaring them. These types are verified by the compiler as you build your project. However, doing this can in some cases feel redundant, introduce technical debt or just not look nice. A common example might be:

```csharp
MyCoolNewClass coolClass = new MyCoolNewClass();
```

In order to avoid duplicating `MyCoolNewClass` in your code you may use C#'s `var` keyword when doing this in local fields (inside of functions for example). `var` automatically detects the type from the right hand side of your assignment and creates a new variable/field with the given type.

This isn't like creating a dynamic type - you don't lose C#'s static typing - instead the type is detected at compile time for you ensuring that you still get intellisense code suggestions, compile errors and warnings and more without having to type a little extra.

This doesn't require a constructor on the right hand side to work either. Function output is detected and will function as you'd expect in this case as well.

Because the results of this are still statically typed you must have a right hand value when using `var`.

```csharp
var myField;
```

Is not valid because the type can't be detected.

* Note: This trick only works on local fields. *

Read more about how implicit typing in C# works on MSDN: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables

Join the World of Zero Discord server: discord.gg/hU5Kq2u
Using Var - Implicitly Typed Fields - Quick TipCalculating Dice Sides From A Meshs NormalsAdding Unity Support - SignalR in Unity - Part 2My Time Learning Containerized Developer Environments - Live StreamGo Go Godot: New ProjectLets Make An Expanding Pipe ShaderUsing Scriptable Objects in UnityImproving Object References - LINQ for Unity Stream - Part 1Loading Levels From Textures in Unity 3DAn Intro to Json Serialization and Deserialization in UnityMy .STL Generator Breaks in Germany (and other countries), Lets Fix ThatLive Coding Handles and Custom Editors in Unity
World of Zero |

Using Var - Implicitly Typed Fields - Quick Tip

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER