C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# Implicitly Typed Local VariableIn C#, we can create local variable without specifying its type. The C# var keyword is used to create implicit typed local variables. The C# compiler infers the types of variable on the basis of assigned value. The var keyword can be used in following context.
The var keyword has following restrictions.
Let's see an example. Here, we have created integer, string and array type local variables. C# Implicit Typed Local Variable Exampleusing System; namespace CSharpFeatures { class ImplicitTypedExample { public static void Main() { // integer var a = 20; // string var s = "TheDeveloperBlog"; // array var arr = new[] { 1,2,3}; Console.WriteLine(a); Console.WriteLine(s); Console.WriteLine(arr[2]); } } } Output: 20 TheDeveloperBlog 3
Next TopicC# Object and Collection Initializer
|