Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- // Static and non-static fields with different names.
- static int x;
- int y;
- // Static and non-static fields with the same name.
- static int i;
- int i;
- // A generic non-static method so we can access static fields and
- // non-static fields within the method.
- void Foo() {
- // Accessing the static field x is no problem.
- x = 10;
- Program.x = 10;
- // Accessing the static field through our current instance
- // does not work. No issue here; there is no non-static field x.
- this.x = 10;
- // Accessing the non-static field y is no problem.
- y = 10;
- this.y = 10;
- // Accessing the non-static field y through our class name
- // does not work. No issue here; there is no static field y.
- Program.y = 10;
- // Try to access i. Ambiguity error. Naturally; it could be trying
- // to reference the non-static i, or the static i.
- i = 10;
- // Try to access the non-static i with the current instance.
- // Ambiguity error?...
- this.i = 10;
- // Try to access the static i with the class name.
- // Ambiguity error?...
- Program.i = 10;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement