T
The Petar
I am really unhappy that C# accessors are not powerful
enough to mimic as if you were accessing an class field.
In particular, let S be:
public struct S {
public int v;
public void change () { v = 99; }
}
Let C be:
public class C {
public S s;
}
Then I could change the value of s, by soing:
C c;
c.s.change ();
If, instead, I wanted to run access to s through an accessor:
public class C {
private S s;
public S s_accessor {
get { return s; }
set { s = value; }
}
}
Then the expression:
C c;
c.s_accessor.change ();
will not have the same effect as:
C c;
c.s.change ();
I find this to be a big flaw. I.e. accessors cannot mimic the behavior
of
value typed fields properly. I also think this flaw is unfixable, b/c
Microsoft
itself exhibits this flaw in its own implementation of Nullable types:
class Test {
public struct S {
public int v;
public void ch () { v = 88; }
}
public static void Main () {
S y;
y.v = 0;
S? x = y;
x.Value.ch ();
Console.WriteLine ("{0}", x.Value.v);
}
}
Prints out "0", instead of the naturally expected "88".
enough to mimic as if you were accessing an class field.
In particular, let S be:
public struct S {
public int v;
public void change () { v = 99; }
}
Let C be:
public class C {
public S s;
}
Then I could change the value of s, by soing:
C c;
c.s.change ();
If, instead, I wanted to run access to s through an accessor:
public class C {
private S s;
public S s_accessor {
get { return s; }
set { s = value; }
}
}
Then the expression:
C c;
c.s_accessor.change ();
will not have the same effect as:
C c;
c.s.change ();
I find this to be a big flaw. I.e. accessors cannot mimic the behavior
of
value typed fields properly. I also think this flaw is unfixable, b/c
Microsoft
itself exhibits this flaw in its own implementation of Nullable types:
class Test {
public struct S {
public int v;
public void ch () { v = 88; }
}
public static void Main () {
S y;
y.v = 0;
S? x = y;
x.Value.ch ();
Console.WriteLine ("{0}", x.Value.v);
}
}
Prints out "0", instead of the naturally expected "88".