Click here to Skip to main content
15,892,804 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
      }
  }
Posted
Updated 8-Apr-13 2:54am
v2
Comments
CHill60 8-Apr-13 8:58am    
You've done it. What is the problem?
Anderso0on 8-Apr-13 9:04am    
the object in Main function does not equal null
CHill60 8-Apr-13 9:18am    
Instead of NewObj = null; try _Obj = null;
CHill60 8-Apr-13 9:20am    
By the way that was not my downvote on the question. @Downvoter - please add a comment as to why you are downvoting the question - it will help the OP to learn
Anderso0on 8-Apr-13 9:26am    
class AAA
{
public int X { get; set; }
public int Y { get; set; }

}
class CCC
{
AAA OBJ;
public CCC(ref AAA _obj)
{
OBJ=_obj;
}
void Fun()
{
OBJ = null;
}
}
class Program
{
static void Main(string[] args)
{
AAA obj = new AAA();

CCC obj2=new CCC(ref obj);
}
}

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
C#
static void Fun(ref AAA _Obj)
  {
    _Obj = null; 
  }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Apr-13 9:32am    
Of course. A 5. I answered the same and also tried to explain something about it...
—SA
Sergey Alexandrovich Kryukov 8-Apr-13 9:34am    
I think your formatting was not what you wanted to express, so the quote was not separated from your words. I tried to fix it; if you don't mind.
—SA
CPallini 8-Apr-13 10:20am    
Thank you for fixing it.
Sergey Alexandrovich Kryukov 8-Apr-13 10:44am    
You are very welcome.
—SA
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:
C#
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
 
Share this answer
 
Comments
Anderso0on 8-Apr-13 9:35am    
class AAA
{
public int X { get; set; }
public int Y { get; set; }

}
class CCC
{
AAA OBJ;
public CCC(ref AAA _obj)
{
OBJ=_obj;
}
void Fun()
{
OBJ = null;
}
}
class Program
{
static void Main(string[] args)
{
AAA obj = new AAA();

CCC obj2=new CCC(ref obj);
}
}
Anderso0on 8-Apr-13 9:35am    
if i do that , how i set obj=null
Sergey Alexandrovich Kryukov 8-Apr-13 9:50am    
What does it mean? You can only make some reference null. You got two answers on how to do it:
obj = null; // done :-)
—SA
Sergey Alexandrovich Kryukov 8-Apr-13 9:56am    
Read the answers. In your original variant, Fun should be static (making it non-static would be redundant).

Not Fun is non-static (instance function), and OBJ is a member, so Fun has access to the member, no need to have ref parameter.
Oh, now you have ref parameter in constructor. In makes no sense at all. You are not modifying referenced object at all.
Fun modifies the reference OBJ, but this is only a copy of _obj, which you don't touch.

I already explain you everything. You are going nowhere. First think over existing answer and draw conclusion, instead of blunt trials.
And please accept the answers formally (green button).
CPallini 8-Apr-13 10:21am    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900