In VB.net, I can write:

If {"red", "blue"}.Contains("blue") Then Return True

and the Contains seems to be from Linq.Enumerable(Of T).

I'm having trouble converting it to C# - when I use an online conversion tool like the one from Developer Fusion, it gives me:

if ({"red", "blue"}.Contains("blue")) return true;

but it doesn't compile, saying it's unable to resolve the symbol Contains which isn't very helpful. I'm sure it's a simple syntax issue, but I'm not sure what you call an example like this.

I don't need to instantiate the array, since I'm just using it to evaluate the expression inline. This seems to be possible in VB.NET. What do you call this - a static array? constant array? anonymous array? some combination of those listed?

I'd like to know how to write this in C#, and also what this is called (I'll update the question title and tags to better reflect what I'm asking when someone can answer that). Thanks!

有帮助吗?

解决方案

This would be your direct conversion

if (new []{"red", "blue"}.Contains("blue")) return true;

Oh, it's called an array initializer

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top