日期:2014-05-18  浏览次数:21057 次

HashTable的引用是否可以用“传值”/“传参”来区分?
一个自定义的Attribute类

  class Attribute : IAttribute
  {
  Hashtable _IndexNameTable;
  Hashtable _Table;

  ...

  public void Add(string name, string value)
  {
  //_Table = new Hashtable();
  _Table.Add(name, value);
  _IndexNameTable.Add(_IndexNameTable.Count, name);
  }
  //初始化HashTable
  private void initHashTable(ref Hashtable ht)
  {
  if (ht == null)
  ht = new Hashtable();
  }

  public Attribute() 
  {
  initHashTable(ref _IndexNameTable);
  initHashTable(ref _Table);
  }
  }
调用该类
  Attribute att = new Attribute();//实例该类的时候,应该执行了两个initHashTable方法来初始化两个HashTable.
  att.Add("a", "aaaa");

我在initHashTable方法中不使用ref的话,_IndexNameTable/_Table仍为null.请问HashTable也是传值的吗?麻烦讲解得稍微详细点。


------解决方案--------------------
ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
------解决方案--------------------
探讨
谢谢,ref没问题,我主要想了解HashTable量在传递过程中为什么不是一个指针。