The community is working on translating this tutorial into Thai, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
If you are fluent in Thai, then please help us - just point to any untranslated element (highlighted with a yellow left border - remember that images should have their titles translated as well!) inside the article and click the translation button to get started. Or have a look at the current translation status for the Thai language.
If you see a translation that you think looks wrong, then please consult the original article to make sure and then use the vote button to let us know about it.
Please help us by translating the following metadata for the article/chapter, if they are not already translated.
If you are not satisfied with the translation of a specific metadata item, you may vote it down - when it reaches a certain negative threshold, it will be removed. Please only submit an altered translation of a metadata item if you have good reasons to do so!
Implicitly typed variables (the var keyword)
As of C# version 3.0, you can use the var keyword on the left side of a variable declaration, instead of explicitly declaring the type of the variable. This is only possible inside a method - not on the class level, where you always have to specify the type. Let's see how it looks when you use the var keyword:
int age = 42; // Explicitly typed variable
var name = "John Doe"; // Implicitly typed variable
Two variables - the one is declared with the int type, while the other one is declared with the var keyword, instead of specifying it as a string. Notice that I'm assigning a value in both cases, because while this is not required when declaring a type, it IS required when using the var keyword. The reason is that the C# compiler will infer the type from the right part of the statement - it simply looks at what you're trying to assign to the variable during the compile and then changes the var keyword into the appropriate type.
This also means that there is no overhead when using the var keyword - it's just as fast during runtime as an explicitly declared variable, because that's essentially what it is when the .NET framework executes your code.
Our first example is very trivial - there's not a whole lot of time saved in writing "var" instead of "string". However, sometimes you will be declaring much more complex types, either as a new variable or as the local result from a function (user-defined or from the framework). There's a chance to save a fair amount of keystrokes in an example like this one:
Dictionary<int, List<string>> dict1 = new Dictionary<int, List<string>>();
var dict2 = new Dictionary<int, List<string>>();
The result will be exactly the same, but you definitely save some keystrokes in the latter example, and since you specify the type on the right side in both cases, there's pretty much no loss in readability of your code when using the var keyword in an example like this.
You can also use the var keyword when declaring a local variable as the result of a method call:
var s = DateTime.Now.ToString();
Again, it's faster and it's still pretty clear what happens and which type the variable will hold (a string). That might not be in a case like this though:
var myVar = SomeMethodWithANameWhichDoesntIndicateTheReturnType();
In a situation like this, it's not clear at all what the variable will contain and you might be sacrificing the readability of your code. You might want to consider use an explicitly typed variable here.
The var Keyword & Anonymous Types
So far, the examples we have seen with the var keyword has been mostly from the "syntactical sugar" department - they are nice to have and shorter to type, but not really a requirement. However, when working with anonymous types (more on them later), it makes sense to declare your objects with the var keyword:
var myObj = new
Name = "John Doe",
Age = 42
In this case, you really need the var keyword to later access the fields of the anonymous type, as shown in this example.
The var keyword allows you to declare a local variable (inside a method or a loop) without explicitly stating the type - instead, the C# compiler infers the type from the right part of the declaration. This can be really convenient in a lot of situations, but it might also make your code slightly less readable. You can use it, or stick to explicitly declaring the variable types - whatever you prefer, but you need it for anonymous types, as already explained.