

Var enumerator = strings.GetEnumerator() Īs in the previous case, the GetEnumerator ( strings.GetEnumerator) call occurs.
#Jaspersoft studio query parameter not found code
We may rewrite this code as follows: static void Foo4(Wrapper wrapper) operator: static void Foo3(Wrapper wrapper) Now let's take a look at what happens in the foreach loop's enumerable expression with the ?. If this reference is null, we get NullReferenceException. In black and white (although it depends on your color scheme), the code says that when the GetEnumerator method is called, the reference is dereferenced. The collection.GetEnumerator() is the key element here. I think we'll have another article about the possible optimizations of the foreach loop. In some cases, foreach loop's IL code may become identical to the code for the for loop. Var enumerator = collection.GetEnumerator() It would look something like this: void Foo2(IEnumerable collection)

IL code suggests that you can rewrite the above code fragment in C# without foreach. Now let's take a look at the foreach loop. Why is it dangerous to use the '?.' operator in the foreach loop's enumerable expression?įirst, let's recall what the ?. This may help to avoid confusion when we talk about different expressions. In this article we use the term "enumerable expression". The C# Specification uses the term "expression" to denote the following entity. We'll return to this task at the end of the article when we have more information. In any case, we'll try to figure this out below. But is that really so? The article's title should have already planted a seed of doubt in your mind. Have you got any ideas on how each of the loops will run? Case #2 with ?. Take a look at the following code: void ForeachTest(IEnumerable collection)įoreach (var item in collection.NotNullItems())įoreach (var item in collection?.NotNullItems()) That is, it can create an illusion of safety when used in the foreach loop.

However, today's article shows that the '?.' operator may be tricky sometimes. Do you like the '?.' operator? Well, who doesn't? Many people like these concise null checks.
