Sunday, 15 July 2012

Index safety when using C# indexers? -


I am coding a vector square in C # and felt that the indexers would be a good addition. Need to worry about the index being out of range?

Perhaps a code sample would be obvious:

  square vector 3f {public vectors 3f (float x, float y, boat z) {this.x = x; this. Y = y; This.Z = z; } Public name X {get; Set;} public float y {received; Set;} public name Z {get; Set;} public this [insot] float {{switch (pause)} {case 0: return it. X; break; Case 1: Return it back. break; Case 2: Return it. break; }} Set {switch (pause) {case 0: this.X = value; break; Case 1: This Y = value; break; Case 2: It's Z = value; break; }}}}  

Should I put the default case in my switch statement? What should it do?

EDIT: This is a silent question, the code above will not be compiled even without the code default plus Jason is superb under implementation.

This is very clear and eliminates the need to worry about range investigations: Public Enum Coordinate {X, Y, Z} floats this [coordinate coordination] to the public {{switch (coordination) {Case Coordinate. X: this return.X; Case coordinates Y: Back it up Y; Case coordinates Z: Return this .Z; // Explain to the compiler that we have covered everything by default: new logic: throwoff exception ("coordinate"); }} Set {switch (coordinate) {case coordinates. X: this.X = value; break; Case coordinates Y: It Y = value; break; Case coordinates Z: this.Z = value; break; }}}

But note that the coordinates are already public, so what does the index mean?


No comments:

Post a Comment