Question

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!

Était-ce utile?

La solution

This would be your direct conversion

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

Oh, it's called an array initializer

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top