Suppose I have a struct and a class with the same members:
using System;
class app
{
static void Main()
{
foo f = new foo() { a = 4, b = 7 };
bar b = f;
Console.WriteLine(b.a);
Console.ReadKey();
}
struct foo
{
public int a { get; set; }
public uint b { get; set; }
}
class bar
{
public int a { get; set; }
public uint b { get; set; }
public static implicit operator foo(bar b)
{
return b;
}
public static implicit operator bar(foo f)
{
return f;
}
}
}
So in short, the members are the same, but the class defines implicit conversion methods to and from the struct.
This code happily compiles as is even though I didn't specify how the values should be converted. My first thought was "The member names/types are the same, so the compiler must be able to figure it out"...
..., but then I changed one of the member's return type from int
to string
and renamed it leaving no trace of the original name, the code still compiled fine.
Please explain the behaviour to me. (Does the environment try to "copy" as many members as possible?)
I use the latest .Net framework in VS 2017 Community