According to C# Coding Conventions, "Don't use implicit typing to determine the type of the loop variable in foreach loops."
But no clear reason is written here, so I don't know why we should use foreach (char ch in laugh) instead of foreach (var ch in laugh).
You can find usually easily which type the var is, as Intellisense suggests like other vars.
Dictionary<Foo, Bar> X = new();
// Add some elements to X...
foreach(KeyValuePair<Foo, Bar> Y in X)
{
// Some Process here
}
Above might be difficult to guess, but that is what the C# implementation of Dictionary is, so not hard to remember.
Is this rule relevant to the note below it("Be careful not to accidentally change a type of an element of the iterable collection...")?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
According to C# Coding Conventions, "Don't use implicit typing to determine the type of the loop variable in foreach loops."
But no clear reason is written here, so I don't know why we should use
foreach (char ch in laugh)instead offoreach (var ch in laugh).You can find usually easily which type the
varis, as Intellisense suggests like othervars.Above might be difficult to guess, but that is what the C# implementation of
Dictionaryis, so not hard to remember.Is this rule relevant to the note below it("Be careful not to accidentally change a type of an element of the iterable collection...")?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.