1

In C# you can to implicit conversion to and from a class object, as shown below:

  public partial class Vector3
  {
    public Vector3(float x, float y, float z)
    {
      X = x;
      Y = y;
      Z = z;
    }

    public static implicit operator Position(Vector3 t)
    {
      return new Position
      {
        X = t.X,
        Y = t.Y,
        Z = t.Z
      };
    }
  }

Where Position is a different class

In my code, I have two Enums, (that are almost identical) but in different namespaces. Unfortunately I cannot get rid of one.

Enum Enum1
{
   A,
   B
}

Enum Enum2
{
   A,
   B
}

I want to create some sort of implicit conversion that permits the follow line below, without requiring any sort of casts:

Enum1 test = Enum2.A;

Currently, everywhere in my code I have to do something like this: Enum1 test = (Enum1)(int)Enum2.A;

Is this possible to do?

Ilan Keshet
  • 176
  • 1
  • 8

0 Answers0