方法使用ref关键字调用inside方法

时间:2017-05-09 11:32:13

标签: c# ref

Hello尝试在另一个方法中调用方法时遇到问题,该方法将ref元素作为参数.Method“replaceWordInLine”创建新的Line,我在方法“findLine”中调用它。我应该给“replaceWordInLine”方法行参数,只是不知道如何用ref做。

findLine(string dataFile, ....)
{
  string[] text = System.IO.File.ReadAllLines(dataFile.....;
  foreach(string line in text)
  {
   replaceWordInLine(ref , disconnectors, word, wordBegining);
  }
}


 replaceWordInLine(ref string e, string disconnectors, string word, int wordBegining)
    {
        findWordInLine(e, s, out word, out wordBegining);
        string findWord = word;
        StringBuilder newLine = new StringBuilder();
        e.Remove(word.Length,pr);
        newLine.Append(word + " " + e);
    }

2 个答案:

答案 0 :(得分:2)

string s = line;
replaceWordInLine(ref s, disconnectors, word, wordBegining);

答案 1 :(得分:2)

您可以使用for i循环而不是foreach

这个:       string [] text = System.IO.File.ReadAllLines(dataFile .....;       foreach(文本中的字符串行)       {        replaceWordInLine(ref,disconnectctors,word,wordBegining);       }

会变成:

  string[] text = System.IO.File.ReadAllLines(dataFile.....;
  foreach(int i =0; i<text.Length; i++)
  {
   replaceWordInLine(ref text[i] , disconnectors, word, wordBegining);
  }

你很高兴