What kind of type explicit is this?

Viewed 99

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 });
1 Answers

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;.

Related