Go has a nice feature where members of a nested struct are automatically accessible via its parent struct:
// Server exposes all the methods that Logger has
type Server struct {
Host string
Port int
*log.Logger
}
// initialize the embedded type the usual way
server := &Server{"localhost", 80, log.New(...)}
// methods implemented on the embedded struct are passed through
server.Log(...) // calls server.Logger.Log(...)
// the field name of the embedded type is its type name (in this case Logger)
var logger *log.Logger = server.Logger
Is the same possible in C#, for example using implicit
casts?
struct B
{
public int x;
}
struct A
{
public B b;
}
var a = new A();
a.b.x = 1; // How it usually works
a.x = 1; // How Go works, assuming x is unambiguous