0

This is my first question on this website as I try to find the information myself exhausting my immediate resources prior to bothering people however I have been unable to find the answer so far. I am taking a Udemy course in which the following code is being used:

foreach (var item in collection)
        {

        }

The instructor was using the object type declaration in the foreach loop instead of the built in "var" type. In my use of the loop regardless of the container class I use to parse through I do not see any difference in which datatype declaration I use, it seems the "var" does not care if the item is an object, simple variable, etc. Why would I use one over the other and does it matter?

Sectess
  • 9
  • 1
  • Does [that](https://stackoverflow.com/questions/20391896/var-versus-concrete-type-usage) answer your question? – Jay Nyxed Jun 17 '21 at 23:55
  • [Here's some more opinions on the use of var](https://stackoverflow.com/questions/3425966/what-advantages-does-using-var-have-over-the-explicit-type-in-c). – Visual Studio Jun 17 '21 at 23:59
  • Slightly and thanks for the response. I understand what that says about why you would use "var" but it talks about using it generically opposed to specific declarations. Unless I am missing something using "Object" instead is basically the same thing as that is also generic use? If my understanding of the two are correct my question would still remain as to why I would choose one over the other? – Sectess Jun 18 '21 at 00:00
  • Hmm, seems again the arguments are implicit vs explicit which I totally understand and would rather go explicit for readability but it seems object and var are both implicit so again looking to see if its just a redundancy thing or if there is a reason to use one over the other. I am really sorry if this is a stupid question its really bugging me because I want to fully grasp the whys... – Sectess Jun 18 '21 at 00:06
  • Here's more about [Object vs Var](https://stackoverflow.com/questions/3221175/var-vs-object-in-c-sharp) – Jay Nyxed Jun 18 '21 at 00:08
  • The _primary_ reason that the `var` keyword even exists in C# is to support **anonymous types**, a feature introduced at the same time as LINQ. There's no way to use an anonymous type without the `var` keyword, because only the compiler knows the actual name of the type. You literally _can't_ write the explicit type. Beyond that scenario, it's strictly a matter of personal preference, and has been hashed out to death on the site already, as you can see by the extensive list of duplicates. – Peter Duniho Jun 18 '21 at 00:18
  • I will also point out that the new _"target-typed expression"_ feature in C# 9 turns the whole `var` conversation upside down, because now you have a way to infer the _right-hand side_ of a variable declaration based on the left-hand side, rather than the other way around as `var` does it. – Peter Duniho Jun 18 '21 at 00:20
  • I can't find a way to select the answer and say it was good but thanks to everyone who answered. The question linked by Jay Nyxed answered it in the best way I could understand. I thought var and object were basically synonyms and redundant but it seems if you use object instead of var you can actually loose method functionality depending on what your passing through. I wish I knew how to upvote for you folks but I don't see a button anywhere in view. Thanks again! – Sectess Jun 18 '21 at 00:23

0 Answers0