2013-06-11 100 views
0

我有使用ref参数的函数有问题。这个函数调用她自己使用linq是这样的:使用ref参数调用函数

public Champ(tabloidConfigColonne tcc,ref Dictionary<string, Jointure> jointures) 
      { 
       ... 
      sousChamps = lc.ToDictionary(
          o => o.nom, 
          o => new Champ(o, ref jointures)); 
      } 

一个错误出现,说ref在匿名函数中不可用。

全功能在这里

public Champ(tabloidConfigColonne tcc,ref Dictionary<string, Jointure> jointures) 
     { 
      nom = tcc.champ; 
      description = tcc.titre; 
      type = tcc.type; 
      valeurDefaut = tcc.valeurParDefaut; 
      modeEdition=new Template(tcc.editeur, tcc.editeurParam1, tcc.editeurParam2, tcc.editeurParam3); 
      if (!string.IsNullOrEmpty(tcc.jointure)) 
      { 
       jointure = jointures[tcc.jointure]; 
       nomTable = jointure.nomNouvelleTable; 
      } 

      visu=tcc.visu; 
      Groupe=tcc.groupe; 
      Id=tcc.nom; 

      valideurs = tcc.valideurs; 
      Obligatoire = tcc.obligatoire; 

      if (tcc.colonnes.Count>0) 
      { 
       List<tabloidConfigColonne> lc = tcc.colonnes.GetColonnes(visibiliteTools.getFullVisibilite(),false); 
       sousChamps = lc.ToDictionary(
        o => o.nom, 
        o => new Champ(o, ref jointures)); 
      } 
     } 

感谢您的帮助。

+13

是的,你不能在匿名函数使用'ref'。你明白为什么吗?看起来你不应该首先使用'ref' - 它不像你改变'Champ'构造函数中的值。请阅读http://pobox.com/~skeet/csharp/parameters.html –

+0

删除'ref',你的代码将被编译。你没有分配''jointures'',所以传递'ref'在你的代码中并不重要(对'Dictionary'的引用将被值传递,这在你的情况下是完全正确的)。 – dasblinkenlight

+0

不知道为什么你会使用ref来传递字典,作为引用类型,除非你需要实际更新指向新对象的指针是不必要的。 – Ashigore

回答