2

I used to use almost all the time the implicit type but since someone told me that using the keyword var was a bad practice I'm now trying to use the explicit one. For this I tend to use the shortcut Ctrl+. to change any implicit type to its explicit one.

Nevertheless, today I had to deal with two lists in one single Foreach. For this I used .Zip() method. I tried changing its implicit type to the explicit one, but I can't. I also try to use the shortcut I mentioned but for some reason there wasn't the option.

var test = enemyTeam.Zip(imageList, (c, i) => new { Champion = c, Image = i });

Test1 (implicit type to Int type):


enter image description here

Test2 (implicit type to explicit type): I can't.


enter image description here

enter image description here

I also tried this implicit type but didn't work:

IEnumerable<new {  CurrentGameParticipant Champion, PictureBox Image }> test = enemyTeam.Zip(imageList, (c, i) => new { Champion = c, Image = i });
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Omar
  • 374
  • 1
  • 14
  • 4
    Did this "someone" give you any argument why they consider `var` bad practice? – Klaus Gütter Jun 23 '21 at 04:06
  • @KlausGütter Because it makes the code more redeable. (Which it makes sense, but also longer/noisier) – Omar Jun 23 '21 at 04:09
  • 2
    My counterargument to "it makes the code more readable" is that, if it does so, your variable and method names are inadequate. Anyway, it's up to the individual team or developer to decide how they feel about `var`. – ProgrammingLlama Jun 23 '21 at 04:11
  • 2
    whoever told you that: tell them that [var is officially recommended _best_ practice](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions#implicitly-typed-local-variables) - and, especially with _long_ class names like `ArbitraryAbstractNonsensicalReasonFactoryProvider`, which _do_ occur far to often, it's very arguably that `var` is better readable by far. (ReSharper/Rider even offers a code inspection to automatically change code to use var) – Franz Gleichmann Jun 23 '21 at 05:19
  • 3
    "*using the keyword var was a bad practice*" => opinion: in addition to the Peter's answer see for example section *Item 1* in the book *Effective C#* by Bill Wagner and [Var Keyword In C#](https://www.c-sharpcorner.com/UploadFile/5ef30d/var-keyword-in-C-Sharp-programming/) and [When to Use and Not Use Variable Type var in C#](https://intellitect.com/when-to-use-and-not-use-var-in-c/). –  Jun 23 '21 at 06:17
  • "Never use `var`" is about as bad as "Always use `var`". – Etienne de Martel Jun 23 '21 at 14:29

1 Answers1

6

new { Champion = c, Image = i } is an anonymous type, and is the entire reason that the var keyword exists in the first place.

You can't give an explicit name. It's anonymous. See Use of var keyword in C# for more details about why var exists, how to use it.

It is possible you might prefer to use value tuples instead; e.g. (c, i) => (Champion: c, Image: i). This is similar to anonymous types, but you can explicitly declare the variable using a value tuple type, such as IEnumerable<(CurrentGameParticipant, PictureBox)> test; or IEnumerable<(CurrentGameParticipant Champion, PictureBox Image)> test;.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136