Implicit typing lets the computer figure out the type of a variable for you. This makes your code shorter and easier to read.
Implicit typing with var keyword in C Sharp (C#)
var variableName = value;
The var keyword tells the compiler to figure out the type from the value on the right side.
You must assign a value when you declare a var variable, so the type can be determined.
number is an int because 10 is an integer.var number = 10;name is a string because of the quotes.var name = "Alice";list is a List<string> from the right side.var list = new List<string>();
This program shows how var automatically sets the type for different variables. It prints the values to the screen.
using System; using System.Collections.Generic; class Program { static void Main() { var age = 25; var city = "New York"; var scores = new List<int> { 90, 85, 88 }; Console.WriteLine($"Age: {age}"); Console.WriteLine($"City: {city}"); Console.WriteLine("Scores:"); foreach (var score in scores) { Console.WriteLine(score); } } }
You cannot declare a var variable without assigning a value.
Once the type is set by the compiler, you cannot change it later.
Using var does not mean the variable is dynamically typed; the type is fixed at compile time.
Implicit typing with var lets the compiler decide the variable type.
It makes code shorter and easier to read when the type is clear from the value.
You must assign a value when declaring a var variable.
Practice
var keyword do in C#?Solution
Step 1: Understand the purpose of
Thevarvarkeyword tells the compiler to infer the variable type from the value assigned.Step 2: Compare options with this understanding
Only It lets the compiler decide the variable's type based on the assigned value. correctly describes this behavior. Other options misunderstandvarusage.Final Answer:
It lets the compiler decide the variable's type based on the assigned value. -> Option AQuick Check:
Implicit typing = compiler infers type [OK]
- Thinking var means no type is assigned
- Assuming var creates constants
- Believing var is a type itself
var in C#?Solution
Step 1: Check syntax rules for
Variables declared withvarvarmust be initialized immediately with a value.Step 2: Evaluate each option
var number = 10; declares and initializes correctly. var number; misses initialization. var = 10; has wrong syntax. var int number = 10; mixes explicit type with var incorrectly.Final Answer:
var number = 10; -> Option DQuick Check:
var requires immediate assignment [OK]
- Declaring var without assigning a value
- Using var as a type name
- Combining var with explicit type
var message = "Hello"; var count = 5; Console.WriteLine(message + " " + count);
Solution
Step 1: Understand variable types inferred
message is a string "Hello", count is an int 5.Step 2: Analyze the concatenation in WriteLine
Using + with string and int converts int to string and concatenates with a space in between, so output is "Hello 5".Final Answer:
Hello 5 -> Option CQuick Check:
String + int concatenates as string [OK]
- Expecting no space between words
- Thinking + prints literal plus sign
- Assuming compilation error due to var
var x; x = 10;
Solution
Step 1: Recall var declaration rules
Variables declared withvarmust be initialized immediately.Step 2: Check the code snippet
Here,var x;declares without initialization, which is invalid.Final Answer:
var requires initialization at declaration. -> Option AQuick Check:
var must be assigned when declared [OK]
- Thinking var can be declared without value
- Confusing var with dynamic
- Assuming assignment later is allowed
result and what will be its value?var numbers = new[] {1, 2, 3, 4};
var result = numbers.Select(n => n * 2).ToList();
Console.WriteLine(result.Count);Solution
Step 1: Understand the type inferred for numbers
numbers is an int array: int[].Step 2: Analyze the LINQ Select and ToList
Select doubles each number, producing IEnumerable<int>. ToList converts it to List<int> with 4 elements.Step 3: Check Console.WriteLine output
result.Count returns 4, the number of elements in the list.Final Answer:
result is a List<int> with 4 elements; output: 4 -> Option BQuick Check:
var infers List<int>, Count = 4 [OK]
- Assuming result is an array, not a List
- Expecting output to be sum or doubled value
- Thinking var causes compilation error here
Ready to prove it?
Pass the test and this pattern moves unknown
