-1
int x = a number
string y = something
bool z = true or false
var x/y/z = anything

Why bother using all these types while we can use var, and it would work just the same. I have read somewhere that it makes the code easier to understand for the reader? But I don't get it! Isn't the point of programming the functionality of what you write? I mean, who cares if my code is hard to read as long as it works... So on the functionality side of the code, does it make any difference? I emphasize that I don't care about other people not understanding what I wrote!

P.S: it's been almost a month since I started learning to program, and c# is my language of choice; So excuse me if my question seems stupid to you experienced ones.

P.S 2: As I said, I searched for the answer and did not find anything from the functionality point of view.

  • It doesn't make a difference and it's a matter of opinion and style. `var` wasn't introduced until C# 3, though, and old habits die hard. – AKX Feb 17 '21 at 07:14
  • 3
    For me personally I use `var` when the type is very clear, and I use the actual type when it's not. when initialising a new object (`new XXX()`) it makes perfect sense, but when calling a random function (`GenerateThumbnails()`) the type could be one of many many things, so I prefer using the actual type, despite the type being in the function signature. So usually it's to avoid repeating myself when initialising collections, arrays, or when types are clear as day (var x = 1, var text = "xxx"). – Jason Rebelo Neves Feb 17 '21 at 07:18
  • (Also worth pointing out that you yourself might be the "next guy" if you have to return to code you wrote 6 months ago and spend days figuring it out again, because so much has happened in the interim you plain forgot what you did) – Caius Jard Feb 17 '21 at 07:21
  • 2
    Your question has a bit of a ranty tone, like you're arguing back against points that haven't even been made yet.. kinda like that guy whose car gets a flat in the middle of nowhere and he has no jack, and by the time he walks miles to the nearest farmhouse he is so convinced that the farmer will refuse to help him he shouts "forget it, I never wanted to borrow your stupid jack anyway" as soon as the farmer opens the door.. Just wanted to point out that SO isn't a place where questions are discussed, debated and argued: we do concise questions that can be answered factually and without emotion – Caius Jard Feb 17 '21 at 07:25
  • Does this answer your question? [var versus concrete type usage](https://stackoverflow.com/questions/20391896/var-versus-concrete-type-usage) – T.kowshik Yedida Feb 17 '21 at 07:26
  • 1
    "who cares if my code is hard to read as long as it works" by this statement I recon that you haven't had much oportunity to maintain legacy code written by someone else. Then wait until that moment and experience will answer this question for you. – Mong Zhu Feb 17 '21 at 11:06
  • 1
    @CaiusJard thank you for your point on my tone. I understand I was very frustrated at the time, not knowing about var then having all those bugs in my code when var could have solved everything for me. Suddenly I discovered it and got very angry, feeling my time was wanted. That was my first question out of frustration, and I will know better from now on. And thanks to all you nice people trying to help me. It is very heartwarming to see all these people are here to help when you doubt that you can make it at the age of 27. – Ahmad Mostafavi Feb 18 '21 at 18:58

1 Answers1

1

The var keyword uses type inference. That means that the compiler uses the value you assign to the variable to identify the type you want to use. When the compiler compiles the code, var name = "xxx" will be converted to string name = "xxx".

I personally prefer the use of var instead of the type because IMHO it makes the code more readable. Especially in cases, where you create a new instance of a class with a long name.

// variable declaration with c#
List<Donaudampfschiffahrtskapitaen> myList = new List<Donaudampfschiffahrtskapitaen>();

// alternative variable declaration starting with c# 3
var myListCs3 = new List<Donaudampfschiffahrtskapitaen>();

// alternative variable declaration starting with c# 9
List<Donaudampfschiffahrtskapitaen> myListCs9 = new(); 
Volkmar Rigo
  • 1,158
  • 18
  • 32
  • Indeed and sometimes we have to use var because there isn't any way of specifying eg anonymous types `var anonTypes = myListCs3.Select(x => new { A = x.ABC });` - some types only the compiler can specify – Caius Jard Feb 18 '21 at 19:33