7

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?

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Sadly, it still gives the same error saying it requires a parameter (after adding the addressOf)
@djerry: I've edited to remove the brackets - see if that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.