为什么以及什么时候我在C#的CLASS SCOPE中做不到的事情?

use*_*808 2 c# variables intellisense scope class

好吧......我很困惑我能做什么,以及在CLASS SCOPE中我做不了什么.

例如

=========================

class myclass
{


  int myint = 0;

  myint = 5; *// this doesnt work. Intellisense doesn't letme work with myint... why?*

  void method()
  {
    myint = 5; *//this works. but why inside a method?*
  } 
}
Run Code Online (Sandbox Code Playgroud)

==================================

class class1
{ 
 public int myint;
}

class class2
{
  class1 Z = new class1();

  Z.myint = 5; *//this doesnt work. like Z doesnt exists for intellisense*

 void method()
  {
    Z.myint = 5; *//this Works, but why inside a method?*
  }
}
Run Code Online (Sandbox Code Playgroud)

多数民众赞成我犯了这么多错误,我不明白什么对班级范围起作用,什么不起作用.

我知道有局部变量及其生命周期.但我不明白这个想法.

JSB*_*ոգչ 8

类只能包含声明,初始化方法.它不能包含自己的语句.

class MyClass
{
    int x; // a declaration: okay
    int y = 5; // a declaration with an initialization: okay
    int GetZ() { return x + y; } // a method: okay

    x = y; // a statement: not okay
}
Run Code Online (Sandbox Code Playgroud)