Is there a way to make an existing type of interface implementation?

Say I'm writing a really good interface. Actually, I think I would like to use some of the built-in types that I use to implement them, so that any code that I write that uses this interface can also use built-in types.

public interface IKickAss
{
    int Yeahhhhhhh() { get; }
}

public static class Woot
{
    public int Bar(IKickAss a, IKickAss b)
    {
        return a.Yeahhhhhhh - b.Yeahhhhhhh;
    }
}

// What I'd like to do, sort of.
public partial struct Int32 : IKickAss
{
    public int Yeahhhhhhh
    {
        get
        {
            return this;
        }
    }
}

. , radix "uint", "IRadixSortable", "uint RadixKey {get}". , : uint, IRadixSortable. , uint IRadixSortable. ... , ?

Haskell (, ), , , . # . , " ":)

+3
6

.

, Duck Typing, . .

+2

, - , . ""

+2

? ICopmarable, IComparer .

, , .

0

, inheritor.

0

, radix , .NET Framework , ..: radix , . . IRadixKeyProvider ( IHashCodeProvider IComparer) RadixKey.

, IRadixKeyProvider , - , - . ( , , , , IRadixKeyProvider , .) , , (, !). , , radix.

0

, (.. - ) .

public interface ISortable
{
    // ... whatever you need to make a class sortable
}

public class ExistingType
{
    // whatever
}

public class NewType : ExistingType, ISortable
{
    // ...
}

unless, of course, if you have access to an existing type ... then just follow its interface.

0
source

Source: https://habr.com/ru/post/1707297/


All Articles