a problem about "bool Exists(Predicate<T> match);"
-
I am trying to convert some code to .net framework 2.0 from .net framework 3.0. the code of .net framework 3.5 is:
if (mItems.Exists(b => string.Compare(b.FileName, item.FileName, StringComparison.OrdinalIgnoreCase) == 0))
return;It can run in .net framework 3.5 and can not run in .net framework 2.0. Could someone tell me where the defferent things between 3.5 from 2.0 in the code are.
-
Lambda expressions were introduced in .Net 3.0. This should be enough:
if (mItems.Exists(string.Compare(b.FileName, item.FileName, StringComparison.OrdinalIgnoreCase) == 0))
return; -
Thanks! There is another problem. The parameter "b" whose type as same as parameter "item" is not defined. And the parameter "b" have not defined anywhere. I try to change the name of parameter "b" to "a",it also could run. I am puzzled about this.
"The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. " So b (and a) are your input parameters.
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
Honestly. It's the honest ones you want to watch out for... -
Lambda expressions were introduced in .Net 3.0. This should be enough:
if (mItems.Exists(string.Compare(b.FileName, item.FileName, StringComparison.OrdinalIgnoreCase) == 0))
return; -
Thanks! There is another problem. The parameter "b" whose type as same as parameter "item" is not defined. And the parameter "b" have not defined anywhere. I try to change the name of parameter "b" to "a",it also could run. I am puzzled about this.
That expression means that A variable "b" would be sent and the expression on the right side of "=>" would be executed on it. Now, you can see through your code and modify it accordingly. You can have a separate method that takes a variable of same type as "b" and returns the result or can declare the variable in the current method/class and use it. If you are using VS2008, you can set the target framework as 2.0 and the original code should work.
-
I am trying to convert some code to .net framework 2.0 from .net framework 3.0. the code of .net framework 3.5 is:
if (mItems.Exists(b => string.Compare(b.FileName, item.FileName, StringComparison.OrdinalIgnoreCase) == 0))
return;It can run in .net framework 3.5 and can not run in .net framework 2.0. Could someone tell me where the defferent things between 3.5 from 2.0 in the code are.
In that line of code, there shouldn't be any differences. Lambda expressions compile to the same IL as anonymous methods in C# 2.0; it shouldn't be the problem. The "Exists" method already was present for List<T> in .NET 2.0, so I don't see why your code would not run on .NET 2.0. Generally, if you use the C# 3.0 compiler without setting any references on .NET 3.x assemblies (e.g. System.Core), the resulting binary will run on .NET 2.0. What does "it can not run" mean? Do you get an exception? What is the error message?
-
That expression means that A variable "b" would be sent and the expression on the right side of "=>" would be executed on it. Now, you can see through your code and modify it accordingly. You can have a separate method that takes a variable of same type as "b" and returns the result or can declare the variable in the current method/class and use it. If you are using VS2008, you can set the target framework as 2.0 and the original code should work.
-
"The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. " So b (and a) are your input parameters.
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
Honestly. It's the honest ones you want to watch out for... -
In that line of code, there shouldn't be any differences. Lambda expressions compile to the same IL as anonymous methods in C# 2.0; it shouldn't be the problem. The "Exists" method already was present for List<T> in .NET 2.0, so I don't see why your code would not run on .NET 2.0. Generally, if you use the C# 3.0 compiler without setting any references on .NET 3.x assemblies (e.g. System.Core), the resulting binary will run on .NET 2.0. What does "it can not run" mean? Do you get an exception? What is the error message?
Thanks! I just back from a business trip. I have googled the lambda operator specifies and research it.There are some differences in syntax. In .net 2.0, it need use delegate to define a virtual method instead of the lambda expressions. "it can not run", I said, means that, I could not debug the code in .net 2.0. And I just want to konw why it was happened. Thank you.