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.
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:
- Implement the
==operator yourself in theWrappedIntclass. (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;
}
- Convert
WrappedIntinto arecord class(orrecord structfor that matter), which implements the operator for you