Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1.     class Program
  2.     {
  3.         // Static and non-static fields with different names.
  4.         static int x;
  5.         int y;
  6.  
  7.         // Static and non-static fields with the same name.
  8.         static int i;
  9.         int i;
  10.  
  11.         // A generic non-static method so we can access static fields and
  12.         // non-static fields within the method.
  13.         void Foo() {
  14.  
  15.             // Accessing the static field x is no problem.
  16.             x = 10;
  17.             Program.x = 10;
  18.  
  19.             // Accessing the static field through our current instance
  20.             // does not work. No issue here; there is no non-static field x.
  21.             this.x = 10;
  22.  
  23.             // Accessing the non-static field y is no problem.
  24.             y = 10;
  25.             this.y = 10;
  26.  
  27.             // Accessing the non-static field y through our class name
  28.             // does not work. No issue here; there is no static field y.
  29.             Program.y = 10;
  30.  
  31.             // Try to access i. Ambiguity error. Naturally; it could be trying
  32.             // to reference the non-static i, or the static i.
  33.             i = 10;
  34.  
  35.             // Try to access the non-static i with the current instance.
  36.             // Ambiguity error?...
  37.             this.i = 10;
  38.  
  39.             // Try to access the static i with the class name.
  40.             // Ambiguity error?...
  41.             Program.i = 10;
  42.         }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement