4 ms·
C# tuple field names are an interesting case, spiritually similar to this proposal's ad hoc unions: persisted in compiled output via attributes, respected by th
by jasomill 2y ago
C# tuple field names are an interesting case, spiritually similar to this proposal's ad hoc unions: persisted in compiled output via attributes, respected by the compiler even across assembly boundaries, but not actually part of the resulting CLR type. So, e.g., if
var p = (x: 1, y: 2);
var q = (u: 1, y: 2);
var f = ((int x, int y) a) => a.x + a.y;
then
p == q
f(p) == 3
f(q) == 3
p.GetType() == q.GetType()
are all true, but
var r = q.x;
yields the compile-time error
CS1061: '(int u, int v)' does not contain a definition for 'x' and no accessible extension method 'x' accepting a first argument of type '(int u, int v)' could be found (are you missing a using directive or an assembly reference?)
and if
dynamic d = q;
then
var s = d.u;
compiles, but throws
RuntimeBinderException: 'System.ValueTuple<int,int>' does not contain a definition for 'u'
at runtime, revealing the underlying CLR type.