我怎样才能使依赖的判决等于零 [英] How i can make referance on referance equal null

查看:63
本文介绍了我怎样才能使依赖的判决等于零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 class AAA 
{
public int X {get;组; }
public int Y {get;组; }

}
class程序
{
static void Main(string [] args)
{
AAA obj = new AAA() ;
Fun(ref obj);
}
static void Fun(ref AAA _Obj)
{
AAA NewObj = null;
NewObj = _Obj;
NewObj = null; //我如何设置obj等于null
}
}

解决方案

引用:

static void Fun(ref AAA _Obj)

{

AAA NewObj = null;

NewObj = _Obj;

NewObj = null; //我如何设置obj等于null

}





如上所述,将上面的代码更改为

 静态  void 乐趣( ref  AAA _Obj)
{
_Obj = null ;
}


您甚至没有尝试将某个对象设为null,而是创建另一个引用并将其设为null。这个空引用本身就是一个堆栈变量(你知道堆栈是如何工作的吗?),所以它在完成对 Fun 的调用后消失了。你没有对引用 AAA 做任何事情,当然也没有对这个引用引用的对象做任何事情。



你可以通过这个简单的方法使引用为null:

  static   void  Nullify( ref   object  @object){
@object = < span class =code-keyword> null ;
}





它也不会对引用的对象做任何事情,但它会修改传递给引用的引用。< br $> b $ b

也许这个问题确实可以帮助你理解事物,但不要指望这个练习可以教你任何有用的技巧或东西。相反,您只需要绘制图片,参考类型如何工作。当您使用具有某些变量或成员的引用类型时,此类变量和成员是单独的对象,引用;但是还有一个引用的对象,由这样的变量或成员引用。



当你为变量或引用类型的成员分配任何东西,甚至是null时,你永远不会修改引用的对象本身;如果使用该对象的成员,则只能修改它。



实际上,通过引用传递引用类型对象几乎没有意义;对象本身,即使通过值传递,也已被引用。而且你不需要为引用分配null;但是,你可以这样做只是为了表明一些条件,稍后用if语句进行检查。



-SA

class AAA
  {
      public int X { get; set; }
      public int Y { get; set; }

  }
  class Program
  {
      static void Main(string[] args)
      {
          AAA obj = new AAA();
          Fun( ref obj);
      }
      static void Fun(ref AAA _Obj)
      {
          AAA NewObj=null;
          NewObj = _Obj;
          NewObj = null; //how i can set obj equal null
      }
  }

解决方案

Quote:

static void Fun(ref AAA _Obj)
{
AAA NewObj=null;
NewObj = _Obj;
NewObj = null; //how i can set obj equal null
}



As already suggested, change the above code to

static void Fun(ref AAA _Obj)
  {
    _Obj = null; 
  }


You are not even trying to make some object null, you create another reference and make it null. This null reference is itself a stack variable (do you understand how stack works?), so it disappears after the call to Fun is done. You do nothing to the reference AAA, and of course nothing to the object referenced by this reference.

You could make a reference null by this simple method:

static void Nullify(ref object @object) {
    @object = null;
}



It also does not do anything to the referenced object, but it does modify the reference passed be reference.

Maybe this question really can help you to understand things, but don''t expect that this exercise can teach you any useful techniques or something. Rather, you need just to draw a picture, how reference types work. When you work with a reference types with some variables or members, such variables and members are separate object, references; but there is also a referenced object, referenced by such variable or member.

When you assign anything, even null, to a variable or a member of a reference type, you never ever modify the referenced object itself; you can only modify it if you use a member of that object.

In practice, passing a reference-type object by reference makes little to now sense; the object itself, even if passed by value, is already referenced. And you don''t need to assign null to a reference; however, you can do it just to indicate some condition, to be later checked up using "if" statement.

—SA


这篇关于我怎样才能使依赖的判决等于零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆