2015-10-14 78 views
-6

这段代码的工作原理,但我不明白其背后的原因(它只是程序的一部分)我的主要关注点是如何填写我的字典读取方法?我没有使用任何参考,但不知何故无效方法返回一个值,它是如何可能的?为什么我的字典没有被返回

我正在阅读3个不同的文件。不知何故,他们都放在单个字典中,有人能告诉我确切的行吗?

 Dictionary<string, Atstovybe> Atstovybes = new Dictionary<string, Atstovybe>(); 

     Atstovybes.Add("InfoSA", new Atstovybe("InfoSA")); 
     Atstovybes.Add("Statius", new Atstovybe("Statius")); 
     Atstovybes.Add("VfSA", new Atstovybe("VfSA")); 

     string[] filepaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.txt"); 

     foreach (string file in filepaths) 
     { 
      SkaitytiDuomenis(file, Atstovybes); 
     } 


    public static void SkaitytiDuomenis(string file, Dictionary<string, Atstovybe> Atstovybes) 
    { 

     string AtstovybePav = null; 

     using (StreamReader reader = new StreamReader(@file)) 
     { 
      string line = null; 
      line = reader.ReadLine(); 
      if (line != null) 
      { 
       AtstovybePav = line; 
      } 

      Atstovybe atstovybe = Atstovybes[AtstovybePav]; 

      while (null!= (line=reader.ReadLine())) 
      { 
       string[] values = line.Split(';'); 

       string tema = values[0]; 
       int sudetingumas = int.Parse(values[1]); 
       string autorius = values[2]; 
       string tekstas = values[3]; 
       string var1 = values[4]; 
       string var2 = values[5]; 
       string var3 = values[6]; 
       string var4 = values[7]; 
       string teisingas = values[8]; 
       int balai = int.Parse(values[9]); 

       Klausimas klausimas = new Klausimas(tema, sudetingumas, autorius, tekstas, var1, var2, var3, var4, teisingas, balai); 

       atstovybe.Klausimai.Add(klausimas); 

      } 


     } 

    } 
+1

将您的代码添加到您的问题中,而不是链接到第三方网站。它提高了在搜索中找到它的能力。 –

+5

听起来像你不明白参考类型如何在.NET中工作 - 我建议你阅读http://jonskeet.uk/csharp/references.html和http://jonskeet.uk/csharp/parameters.html –

+0

谢谢神。至少你没有把它当作bug。 –

回答

-2

atstovybe.Klausimai.Add(klausimas); 是在SkaitytiDuomenis方法,这是通过参考值添加值。

+3

在这里没有“通过参考”。 “通过参考”与“参考文献的行为”相结合对IMO没有帮助。 –

+0

如果你看SkaitytiDuomenis(文件,Atstovybes);在这里你传递字典的对象,这是字典的参考。希望这会有所帮助。 –

+2

是的,这是一个参考。我明白那个。但是使用术语“by ref”是* not *有用,因为它将'ref'参数与参考类型混淆。 –

相关问题