I'm trying to recreate this in C#.
The problem i get is if i use constructors i MUST use new MyInt which i DO NOT WANT (to verbose). The way around it is to use the implicit/explicit operators. However they MUST be public... How the heck do i implement this in feature in C#?
The short question is i'd like to pass byte/short into a function but not int. Passing int should get me a compile error. I know i can easily get runtime with a public implicit int operator. The code below shows that int is automatically converted to char
Running sample shows true
using System;
class Program
{
public static void Write(MyInt v)
{
Console.WriteLine("{0}", v.v is byte);
}
static void Main(string[] args)
{
Write(2);
}
}
public struct MyInt
{
public object v;
public MyInt(byte vv) { v = vv; }
public MyInt(short vv) { v = vv; }
public MyInt(byte[] vv) { v = vv; }
public static implicit operator MyInt(byte vv) { return new MyInt { v = vv }; }
//public static extern implicit operator MyInt(int vv);
}
Heres more code which is useless. It implements MyInt2/MyInt which isn't required in the C++ solution