C
C#2d ago
Faker

when to use var keyword and when to use data type itself

Hello guys, can someone explain when should I use the var keyword and when should I use the actual data type pls.
18 Replies
Faker
FakerOP2d ago
I was trying to use the string keyword to take user input like the following: string name = Console.ReadLine(); But the Console.Readline has been underlined by the IDE indicating a warnimg that it can return a null value. When we change the string to var, it doesn't do that though. I read upon that a bit and what I read it that var expect nullable data types, like string?
mg
mg2d ago
var is a way to avoid writing the type out explicitly when it can be inferred from the right hand side of the assignment see this meme $var
mg
mg2d ago
Console.ReadLine() returns a string?, so your IDE will warn you that trying to assign its return value to a variable of type string could result in a null value in a non-nullable variable if you were to type string? name = Console.ReadLine(), the warning would go away var name = Console.ReadLine() results in the exact same thing, you just don't have to type string?
Faker
FakerOP2d ago
Yeah, I also read that string is expected to be non-nullable but the thing is, it is a referenced type, can't it be assigned to null ? Using only var when required in my code won't make my code less readable ?
mg
mg2d ago
pre C# 8, yes, and post C# 8, still technically yes, but if you're using nullable reference types, you shouldn't be assigning null to non-nullable types
Angius
Angius2d ago
I always use var tbh If I'm curious what type it is, I can hover over it and the IDE will tell me
mg
mg2d ago
you should generally use var everywhere you can
Faker
FakerOP2d ago
ok got it, thanks guys !
mg
mg2d ago
sometimes it's better for readability to specify the type, or necessary when the type can't be inferred from the right hand side, but other than that, var all the way
Faker
FakerOP2d ago
by the way is there a reason for that? I know it results in less typing but any other reason ?
mg
mg2d ago
pretty much that, less typing for you and less text for any readers of your code
Angius
Angius2d ago
why use much word when few word do trick
mg
mg2d ago
like imagine
Dictionary<string, object> myDictionary = new Dictionary<string, object>();
// vs
var myDictionary = new Dictionary<string, object>();
Dictionary<string, object> myDictionary = new Dictionary<string, object>();
// vs
var myDictionary = new Dictionary<string, object>();
second one is easier for both you and a reader
Faker
FakerOP2d ago
yep I see yeah true
Angius
Angius2d ago
Also, visual alignment, helps you scan the code faster.
var foo = GetNumber();
var barbar = something.Name;
var bgdsadasdsad = new Dictionary<string, Person<int>>();
var foo = GetNumber();
var barbar = something.Name;
var bgdsadasdsad = new Dictionary<string, Person<int>>();
vs
int foo = GetNumber();
string barbar = something.Name;
Dictionary<string, Person<int>> bgdsadasdsad = new Dictionary<string, Person<int>>();
int foo = GetNumber();
string barbar = something.Name;
Dictionary<string, Person<int>> bgdsadasdsad = new Dictionary<string, Person<int>>();
The names of the variables are one under another
Faker
FakerOP2d ago
yeah I see noted, thanks 👍
Chris Pulman
Chris Pulman2d ago
The disadvantage of not using var can be the wrong type being returned even though it may be compatible, especially when using nullable types for example string and string?, var will always hold the correct type.

Did you find this page helpful?