0

while trying to compile following code

IEquatable<Object> value = "test";

it causes this error:

Cannot implicitly convert type 'string' to 'System.IEquatable<object>'  

but string implements IEquatable<String>

MyCode:

    public class Equals : Validation
    {
        public IEquatable<Object> Value { get; set; }

        public Equals(IEquatable<Object> value)
        {
            Value = value;
        }

        public override bool Validate(Object value)
        {
            return Value.Equals(value);
        }
    }

Usage

        [Equals("test")]
        public string Name { get; set; }
Sahandevs
  • 1,120
  • 9
  • 25
  • 3
    `IEquatable` is not `IEquatable` – Kevin Gosse Jan 21 '19 at 14:13
  • 2
    Google Covariance and Contravariance and you should find all the info you need or check out this https://stackoverflow.com/questions/9806170/covariance-and-contravariance-in-c-sharp – Dave Jan 21 '19 at 14:14
  • @KevinGosse but String can be casted to object – Sahandevs Jan 21 '19 at 14:14
  • 2
    @Sahandevs It doesn't mean that casting the generic type would be safe. Let's take a simple example: `List`. By following your reasoning, it should be castable to `List`. Therefore, I should be able to add an `int` inside of it (since int is castable to object). You see how unsafe it can get. That's why only interfaces that are explicitly marked as covariant can be casted that way. This is not the case for `IEquatable` – Kevin Gosse Jan 21 '19 at 14:18
  • @Rango is there is anyway to use generics like java? – Sahandevs Jan 21 '19 at 14:18
  • @Rango like IEquatable> – Sahandevs Jan 21 '19 at 14:18
  • @Sahandevs what's your real problem ? what do you need `IEquatable` for ? – Ahmad Ibrahim Jan 21 '19 at 14:21
  • @AhmadIbrahim i updated my post – Sahandevs Jan 21 '19 at 14:24
  • In this precise case, you don't need `IEquatable`. Just store an `Object` and call `Equals(object)` : https://learn.microsoft.com/en-us/dotnet/api/system.object.equals – Kevin Gosse Jan 21 '19 at 14:32
  • @KevinGosse but in case of Max Or Min there is no compareTo method for object and i have to use IComparable – Sahandevs Jan 21 '19 at 14:34
  • This is fundamentally unsafe, if it were allowed you could do: `IComparable comp = "test"; comp(3);`. You can either use the non-generic interfaces such as `IComparable`, create an unsafe wrapper implementing `IEquatable` around some `IEquatable` implementation, or use reflection/dynamic typing. – Lee Jan 21 '19 at 14:43

2 Answers2

1

String implements IEquatable<String> and not IEquatable<Object> So IEquatable<String> value = "test" should work

  • i cannot change the generic type is there anyway to convert these two? string can be casted to object – Sahandevs Jan 21 '19 at 14:16
  • No it is not possible. You can cast string to object but you cant cast `IEquatable` to `IEquatable` because string inherits object but `IEquatable` does not inherit `IEquatable` – Merhat Pandzharov Jan 21 '19 at 14:19
0

for your code to work, you can not insert into an object the value of a string so that it works use:

IEquatable<string> value ="test";
rafael3do
  • 1
  • 2