简体   繁体   中英

How do I convert this VB.NET array expression to C#

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM