3 回答
TA贡献1863条经验 获得超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条经验 获得超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 关注
- 496 浏览
添加回答
举报