简体   繁体   中英

String.Contains doesn't require parameters in c#?

I've stumbled upon this code:

var knownSeparators = new[] { "\\", "/", "|", "." };
return knownSeparators.FirstOrDefault(path.Contains);

where path is a string and the return value should be a string as well.

Allthough path.Contains' intellisense suggests a parameter, it works fine without one.

How does this work exactly? Is there any way to copy this behavior in vb.net?

FirstOrDefault takes a delegate (a Func<T, bool> ) and this call is creating a delegate from the method group . It's equivalent to:

Func<string, bool> predicate = path.Contains;
return knownSeparators.FirstOrDefault(predicate);

I suspect in VB.NET you could do:

Return knownSeparators.FirstOrDefault(AddressOf path.Contains)

... but I couldn't tell for sure without trying it.

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