简体   繁体   English

在C#中,Hashtable默认通过引用或值传递?

[英]In C# Hashtable is passed by reference or by value by default?

我想知道默认情况下是通过引用还是通过值传递的Hashtable?

In C#, all parameters are passed by value. 在C#中,所有参数都按值传递。 But HashTable is a reference type, so the value being passed is a reference to the actual HashTable , if that makes sense. 但是HashTable是一个引用类型,因此传递的值是对实际HashTable的引用,如果这是有意义的。

It thus means the HashTable is not being copied when you pass it as a parameter, but if you try to overwrite the parameter value like so: 因此,当您将HashTable作为参数传递时,它意味着不会复制HashTable,但是如果您尝试覆盖参数值,则:

private void SomeMethod(HashTable ht)
{
   ...
   ht = new HashTable();
   ...
}

then it will not work, because you're overwriting the value parameter, not the actual HashTable . 然后它将无法工作,因为你覆盖了value参数,而不是实际的HashTable

对该哈希表的引用按值传递(除非您指定ref )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM