[英]What is the proper way to set a dictionary value which is a reference type to null?
Let us suppose we have a class like below:-假设我们有一个 class,如下所示:-
class DoubleLinkedListNode
{
public int Value { get; set; }
public DoubleLinkedListNode(int value)
{
Value = value;
}
public DoubleLinkedListNode next = null;
public DoubleLinkedListNode prev = null;
}
And then we create a dictionary as given below:-然后我们创建一个字典,如下所示:-
IDictionary<int, DoubleLinkedListNode> dict = new Dictionary<int, DoubleLinkedListNode>();
The key
of the dictionary will hold the Value
of the DoubleLinkedListNode
that it contains like this:-字典的
key
将保存它包含的DoubleLinkedListNode
的Value
,如下所示:-
DoubleLinkedListNode newNode = new DoubleLinkedListNode(value);
dict.Add(newNode.Value, newNode );
Let us also create a function which takes the type of the value of the dictionary as input and we make that value null
inside the body of the function as shown below:-让我们还创建一个 function ,它将字典值的类型作为输入,我们在 function 的主体内创建该值
null
,如下所示:
private void RemoveNode(DoubleLinkedListNode nodeToBeRemoved)
{
if(nodeToBeRemoved != null)
{
//Do Something
nodeToBeRemoved = null;
}
}
and we call the function like this:-我们这样称呼 function:-
RemoveNode(dict[someValue]);
Let us create another function in which we make the value null
explicitly as shown below:-让我们创建另一个 function ,我们在其中明确地设置值
null
,如下所示:-
private void RemoveNodeAnother(DoubleLinkedListNode nodeToBeRemoved)
{
if(nodeToBeRemoved != null)
{
//Do Something
dict[nodeToBeRemoved.Value] = null;
}
}
And then we call the function like this:-然后我们像这样调用 function:-
RemoveNodeAnother(dict[someValue]);
What is the difference between the above two functions?上面两个函数有什么区别?
Why I am asking is this I was doing this question on Leetcode.我问的原因是我在 Leetcode 上做这个问题。 The solution that I wrote is as follows:-
我写的解决方案如下:-
public class FirstUnique
{
private class DoubleLinkedListNode
{
public int Value { get; set; }
public DoubleLinkedListNode(int value)
{
Value = value;
}
public DoubleLinkedListNode next = null;
public DoubleLinkedListNode prev = null;
}
DoubleLinkedListNode dummyHeadNode = new DoubleLinkedListNode(-1);
DoubleLinkedListNode dummyTailNode = new DoubleLinkedListNode(-1);
IDictionary<int, DoubleLinkedListNode> dict = new Dictionary<int, DoubleLinkedListNode>();
public FirstUnique(int[] nums)
{
InitialiseDummyHeadAndTailNodes();
foreach(int i in nums)
{
Add(i);
}
}
public int ShowFirstUnique()
{
return dummyHeadNode.next.Value;
}
public void Add(int value)
{
if (dict.ContainsKey(value))
{
RemoveNode(dict[value]);
}
else
{
DoubleLinkedListNode newNode = new DoubleLinkedListNode(value);
AddNode(newNode);
}
}
private void InitialiseDummyHeadAndTailNodes()
{
dummyHeadNode.next = dummyTailNode;
dummyTailNode.prev = dummyHeadNode;
}
private void RemoveNode(DoubleLinkedListNode nodeToBeRemoved)
{
if(nodeToBeRemoved != null)
{
nodeToBeRemoved.prev.next = nodeToBeRemoved.next;
nodeToBeRemoved.next.prev = nodeToBeRemoved.prev;
// If I write nodeToBeRemoved = null, the solution won't pass.
// But if I write dict[nodeToBeRemoved.Value] = null, the solution is accepted.
dict[nodeToBeRemoved.Value] = null;
}
}
private void AddNode(DoubleLinkedListNode nodeToBeAdded)
{
//Update the pointers.
nodeToBeAdded.prev = dummyTailNode.prev;
nodeToBeAdded.prev.next = nodeToBeAdded;
nodeToBeAdded.next = dummyTailNode;
dummyTailNode.prev = nodeToBeAdded;
//Add the node to the dictionary.
dict.Add(nodeToBeAdded.Value, nodeToBeAdded);
}
}
I have made the comments where the anomaly lies.我已经在异常所在的地方发表了评论。 What could be the reason for this behavior?
这种行为的原因可能是什么?
There is only one way to "set dictionary value to null" -只有一种方法可以“将字典值设置为空” -
dictionary[key] = null;
indeed, if key is not there you need to add it first... Which leads to the way to set value to null -确实,如果密钥不存在,您需要先添加它...这导致将值设置为 null -
if (dictionary.ContainsKey(key))
dictionary[key] = null;
else
dictionary.Add(key, null);
Note that setting value to null
has zero impact on what was stored there previously.请注意,将值设置为
null
对之前存储的内容的影响为零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.