Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The "==" operator with implicit type conversion. Why does it work only when I define implicit conversion inside A for A->B and not for B->A?

Here’s the code

using System;

public class WrappedInt
{
    private int Value { get; }

    public WrappedInt(int value)
    {
        Value = value;
    }

    // Comparison in Main works when this is defined:
    public static implicit operator int(WrappedInt wInt) => wInt.Value;

    // But doesn't work with this INSTEAD:
    // public static implicit operator WrappedInt(int value) => new WrappedInt(value);

    // Error: Operator '==' cannot be applied to operands of type 'int' and 'WrappedInt'
}


public class Program
{
    public static void Main()
    {
        var a = 2;
        var b = new WrappedInt(2);

        var areSame = a == b;

        Console.WriteLine(areSame);
    }
}

Switching a == b to b == a doesn’t resolve the error, so it’s not about the order. I guess, it has something to do with int being a primitive type.

Post-thought 1

Interestingly, the code below where I compare WrappedInt1 with WrappedInt2 works, even though those objects are implicitly convertible only to int. It’s like the == operator converts those wrapper-objects to ints and then compares ints.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using System;

public class WrappedInt1
{
    private int Value { get; }

    public WrappedInt1(int value)
    {
        Value = value;
    }

    public static implicit operator int(WrappedInt1 wInt) => wInt.Value;
}

public class WrappedInt2
{
    private int Value { get; }

    public WrappedInt2(int value)
    {
        Value = value;
    }
    
    public static implicit operator int(WrappedInt2 wInt) => wInt.Value;
}


public class Program
{
    public static void Main()
    {
        var a = new WrappedInt1(2);
        var b = new WrappedInt2(2);

        var areSame = a == b;

        Console.WriteLine(areSame);
    }
}

Post-thought 2

int indeed seems special when it comes to the == operator because the code below, where I try to compare WrappedInts having only implicit casts to and from another type of WrappedInt, doesn’t compile.

using System;

public class WrappedInt1
{
    public int Value { get; }

    public WrappedInt1(int value)
    {
        Value = value;
    }

    public static implicit operator WrappedInt2(WrappedInt1 wInt) => new WrappedInt2(wInt.Value);
    public static implicit operator WrappedInt1(WrappedInt2 wInt) => new WrappedInt1(wInt.Value);
}

public class WrappedInt2
{
    public int Value { get; }

    public WrappedInt2(int value)
    {
        Value = value;
    }
    
    public static implicit operator WrappedInt2(WrappedInt1 wInt) => new WrappedInt2(wInt.Value);
    public static implicit operator WrappedInt1(WrappedInt2 wInt) => new WrappedInt1(wInt.Value);
}


public class Program
{
    public static void Main()
    {
        var a = new WrappedInt1(2);
        var b = new WrappedInt2(2);

        var areSame = a == b;

        Console.WriteLine(areSame);
    }
}

>Solution :

Your alternative doesn’t work because your class doesn’t define the == operator. The compiler cannot find a "WrappedInt x WrappedInt" version of ==, only one for "int x int", and "object x object".

To make it work as you want, you have a few options:

  1. Implement the == operator yourself in the WrappedInt class. (e.g. with no null checking… note that it will also prompt you to implement != as well):
    public static bool operator ==(WrappedInt a, WrappedInt b)
    {
        return a.Value == b.Value;
    }

    public static bool operator !=(WrappedInt a, WrappedInt b)
    {
        return a.Value != b.Value;
    }
  1. Convert WrappedInt into a record class (or record struct for that matter), which implements the operator for you
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading