Lambda expression

Shortly speaking. I have 2 lists that contain the same type (but are used for different things), and I want to know if the EITHER list contains an element with a specific name.

My original code, which worked prefectly, was:

if (listA.Any(var => var.Name == strMatch) || listB.Any(var => var.Name == strMatch)) { //Do something } 

This code worked fine, regardless of whether this item was present on one or both lists. Later, I had several “impossible” crashes - things that could never happen. I traced it to this if statement, which never returns true.

It pushed me for centuries ... I could not understand what was going wrong. In the end, I gave up and stuck the brackets around the body of the tube expressions, for example ...

 if (listA.Any(var => (var.Name == strMatch)) || listB.Any(var => (var.Name == strMatch))) { //Do something } 

After restarting my program, all the “impossible” errors went away and it functioned normally. Removing unnecessary supports leads to errors.

I have never had this problem with lambda expressions before (especially where they work, and THEN, after several runs work correctly break), and my other lambda expressions work correctly.

Example: The following code works 100% as expected (suppose there is a match in one of the lists)

 Item item = ListA.FirstOrDefault(var => var.Name == strMatch) ?? ListB.FirstOrDefault(var => var.Name == strMatch); 

What's happening? Why is the compiler picky for some lamda expressions and not others? (Even if they are identical?)

UPDATE :: System Information This was related to Microsoft Visual Studio 2008 (Professional), 32-bit Windows Vista.

UPDATE Link to the video , it was a test on other computers and playable NOT . It seems to me that my computer is doomed. Reinstalling VS is not affected.

Please ignore any sounds of a cat's background, it only meows a lot when it hears me recording something.

+4
source share
1 answer

These extra parentheses do not change the meaning of these lambda expressions, nor the way the compiler decides to compile these two different blocks of code. Based on what you have provided, there is no functional difference between your two choices, and therefore the problem is not related to the brackets.

EDIT:

So, I created a quick class that does exactly what you stated, and IL differs only in class names. I used windiff and windiff to confirm this. It also does not change with /optimize- or /debug:full .

  .method /*06000005*/ public hidebysig instance bool '<Main>b__3'(class [mscorlib/*23000001*/]System.Tuple`2/*01000006*/<int32,string> var) cil managed // SIG: 20 01 02 15 12 19 02 08 0E { // Method begins at RVA 0x207c // Code size 22 (0x16) .maxstack 2 .locals /*11000001*/ init ([0] bool CS$1$0000) - .line 29,29 : 69,92 '' + .line 29,29 : 67,88 '' - //000029: if (listA.Any(var => (var.Item2 == strMatch)) || listB.Any(var => (var.Item2 == strMatch))) + //000029: if (listA.Any(var => var.Item2 == strMatch) || listB.Any(var => var.Item2 == strMatch)) IL_0000: /* 03 | */ ldarg.1 IL_0001: /* 6F | (0A)000005 */ callvirt instance !1 class [mscorlib/*23000001*/]System.Tuple`2/*01000006*/<int32,string>/*1B000001*/::get_Item2() /* 0A000005 */ IL_0006: /* 02 | */ ldarg.0 - IL_0007: /* 7B | (04)000001 */ ldfld string Parentheses/*02000002*//'<>c__DisplayClass6'/*02000003*/::strMatch /* 04000001 */ + IL_0007: /* 7B | (04)000001 */ ldfld string NoParentheses/*02000002*//'<>c__DisplayClass6'/*02000003*/::strMatch /* 04000001 */ IL_000c: /* 28 | (0A)000006 */ call bool [mscorlib/*23000001*/]System.String/*01000007*/::op_Equality(string, string) /* 0A000006 */ IL_0011: /* 0A | */ stloc.0 IL_0012: /* 2B | 00 */ br.s IL_0014 

Nota bene: there are two of them: one for each of the calls to listX.Any . Both of them differ only in their comments. The generated IL is identical.

EDIT 2:

The exit from ildasm for Visual Studio 2008 (csc 3.5.30729.4926) is also no different, so I really can’t say why it dies in your VS2k8 instance, except maybe the LINQ version you are listening to or the compiler on this computer has a listener expressions because I cannot reproduce the difference.

+3
source

Source: https://habr.com/ru/post/1335115/


All Articles