简体   繁体   中英

Equivalent of C# .Default. in VB.net?

I found this line of code in another question here on SO:

Comparer<TKey>.Default.Compare(x.Item1, y.Item1);

I'm trying to parse this so I can convert it to VB.net. To start I tried one of the online converters, which produced this:

Comparer(Of TKey).[Default].Compare(x.Item1, y.Item1)

But that returns an error saying that Comparer has no type parameters. Is this because VisualBasic library has a Comparer that is stepping on this one? Or is it because of the [Default] syntax, which I have not seen before?

That is correct, you probably have the name Comparer in scope that is conflicting with System.Collections.Generic.Comparer(Of T) that isn't generic. Check your other modules and imports to see if this is the case.

The square brackets around Default is probably there to ensure that it isn't interpreted as the keyword Default . But it's completely unnecessary here in this context.

Ensure the Comparer is from System.Collections.Generic . And then you can write like this:

Comparer(Of YourClass).Default.Compare(x.Item1, y.Item1)

where x and y's Item1's are of type YourClass.

If the comparer you are using is not from generic collections it will show that error (Comparer has no type parameters).

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