3 回答
TA贡献1863条经验 获得超2个赞
null
DisposeIDisposableusing
using (var ms = new MemoryStream()) {
//...}编辑
实际变量范围
int iVal = 8;//iVal == 8if (iVal == 8){
int iVal = 5;
//iVal == 5}//iVal == 8int 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::ScopeC+作用域和对象生存期
if (true) {
MyClass stackObj; //created on the stack
MyClass heapObj = new MyClass(); //created on the heap
obj.doSomething();} //<-- stackObj is destroyed//heapObj still livesC#对象生命周期
MyClass stackObj;
MyClassMyClassnew
MyClass stackObj = new MyClass();
new
C#对象引用
TA贡献1982条经验 获得超2个赞
DisposeIDisposableDisposeDataSet
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");}aa = nullMainDoSomethingnullDoSomething
- 3 回答
- 0 关注
- 616 浏览
添加回答
举报
