In C++, it is possible to delete this and set its own reference to null.
I want to set an object instance to null itself.
public class Foo
{
public void M( )
{
// this = null; // How do I do this kind of thing?
}
}
In C++, it is possible to delete this and set its own reference to null.
I want to set an object instance to null itself.
public class Foo
{
public void M( )
{
// this = null; // How do I do this kind of thing?
}
}
this is actually just a special name given to the parameter, arg0, in an instance method. Setting it to null is not allowed:
this for instance methods on a classthis for instance methods on a struct, but you can't assign nullThe reason for 1. is that it would not be useful:
arg0 is by-val (not by-ref) on class instance methods, so the method's caller won't notice the change (for completeness: arg0 is by-ref on struct instance methods)null does not delete it; the GC handles thatSo basically, that syntax is not allowed, because it doesn't do what you want. There is no C# metaphor for what you want.
This is not possible in .NET. You cannot set the current instance to null from within the object itself. In .NET the current instance (this) is readonly, you cannot assign a value to it. And by the way that's not something you would even need in .NET.
In C++ delete this frees the memory of the object. There is no equivalent to that in C# (or any other .NET language). Although it is allowed in C++ I don't think it's a good practice. At least you have to be very careful.
.NET uses garbage collection instead to free memory. Once an object isn't referenced any more and cannot be accessed from anywhere in your code the garbage collector can eventually free the memory (and the garbage collector is careful). So just lean back and let the garbage collector do its work.
Simply no. As this is not possible to access a method from a null object. In your case you want say
F f = new F() // where f = null
f.SomeMethod(); // ?????? not possible
In this case you get a Null Reference Exception. You can see Darin's Comment and Explanation too on the same. How could you access anything from null, which means nothing. I have no idea about legacy but .Net does not provides you such things. Instead you can set it to null when its not needed anymore.
Ex From MSDN
public class Node<T>
{
// Private member-variables
private T data;
private NodeList<T> neighbors = null;
public Node() {}
public Node(T data) : this(data, null) {}
public Node(T data, NodeList<T> neighbors)
{
this.data = data;
this.neighbors = neighbors;
}
public T Value
{
get
{
return data;
}
set
{
data = value;
}
}
protected NodeList<T> Neighbors
{
get
{
return neighbors;
}
set
{
neighbors = value;
}
}
}
}
public class NodeList<T> : Collection<Node<T>>
{
public NodeList() : base() { }
public NodeList(int initialSize)
{
// Add the specified number of items
for (int i = 0; i < initialSize; i++)
base.Items.Add(default(Node<T>));
}
public Node<T> FindByValue(T value)
{
// search the list for the value
foreach (Node<T> node in Items)
if (node.Value.Equals(value))
return node;
// if we reached here, we didn't find a matching node
return null;
}
}
and Right—that operate on the base class's Neighbors property.
public class BinaryTreeNode<T> : Node<T>
{
public BinaryTreeNode() : base() {}
public BinaryTreeNode(T data) : base(data, null) {}
public BinaryTreeNode(T data, BinaryTreeNode<T> left, BinaryTreeNode<T> right)
{
base.Value = data;
NodeList<T> children = new NodeList<T>(2);
children[0] = left;
children[1] = right;
base.Neighbors = children;
}
public BinaryTreeNode<T> Left
{
get
{
if (base.Neighbors == null)
return null;
else
return (BinaryTreeNode<T>) base.Neighbors[0];
}
set
{
if (base.Neighbors == null)
base.Neighbors = new NodeList<T>(2);
base.Neighbors[0] = value;
}
}
public BinaryTreeNode<T> Right
{
get
{
if (base.Neighbors == null)
return null;
else
return (BinaryTreeNode<T>) base.Neighbors[1];
}
set
{
if (base.Neighbors == null)
base.Neighbors = new NodeList<T>(2);
base.Neighbors[1] = value;
}
}
}
public class BinaryTree<T>
{
private BinaryTreeNode<T> root;
public BinaryTree()
{
root = null;
}
public virtual void Clear()
{
root = null;
}
public BinaryTreeNode<T> Root
{
get
{
return root;
}
set
{
root = value;
}
}
}