3 回答

TA貢獻1863條經(jīng)驗 獲得超2個贊
null
Dispose
IDisposable
using
using (var ms = new MemoryStream()) { //...}
編輯
實際變量范圍
int iVal = 8;//iVal == 8if (iVal == 8){ int iVal = 5; //iVal == 5}//iVal == 8
int iVal = 8;if(iVal == 8) { int iVal = 5; //error CS0136: A local variable named 'iVal' cannot be declared in this scope because it would give a different meaning to 'iVal', which is already used in a 'parent or current' scope to denote something else}
public static void Scope() { int iVal = 8; if(iVal == 8) { int iVal2 = 5; }}
.method public hidebysig static void Scope() cil managed{ // Code size 19 (0x13) .maxstack 2 .locals init ([0] int32 iVal, [1] int32 iVal2, [2] bool CS$4$0000)//Function IL - omitted} // end of method Test2::Scope
C+作用域和對象生存期
if (true) { MyClass stackObj; //created on the stack MyClass heapObj = new MyClass(); //created on the heap obj.doSomething();} //<-- stackObj is destroyed//heapObj still lives
C#對象生命周期
MyClass stackObj;
MyClass
MyClass
new
MyClass stackObj = new MyClass();
new
C#對象引用

TA貢獻1982條經(jīng)驗 獲得超2個贊
Dispose
IDisposable
Dispose
DataSet
null
public static void Main(){ Object a = new Object(); Console.WriteLine("object created"); DoSomething(a); Console.WriteLine("object used"); a = null; Console.WriteLine("reference set to null");}
a
a = null
Main
DoSomething
null
DoSomething
- 3 回答
- 0 關(guān)注
- 543 瀏覽
添加回答
舉報