使用 ref 关键字在方法内部调用方法
method call inside method with ref keyword
你好,当尝试在另一个方法中调用方法时遇到问题,该方法将 ref 元素保存为 parameter.Method "replaceWordInLine" 创建新行,我在方法 "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);
}
string s = line;
replaceWordInLine(ref s, disconnectors, word, wordBegining);
您可以使用 for i 循环代替 foreach
这个:
string[] text = System.IO.File.ReadAllLines(dataFile.......;
foreach(文本中的字符串行)
{
replaceWordInLine(ref , disconnectors, word, wordBegining);
}
会变成:
string[] text = System.IO.File.ReadAllLines(dataFile.....;
foreach(int i =0; i<text.Length; i++)
{
replaceWordInLine(ref text[i] , disconnectors, word, wordBegining);
}
你可以开始了
你好,当尝试在另一个方法中调用方法时遇到问题,该方法将 ref 元素保存为 parameter.Method "replaceWordInLine" 创建新行,我在方法 "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);
}
string s = line;
replaceWordInLine(ref s, disconnectors, word, wordBegining);
您可以使用 for i 循环代替 foreach
这个: string[] text = System.IO.File.ReadAllLines(dataFile.......; foreach(文本中的字符串行) { replaceWordInLine(ref , disconnectors, word, wordBegining); }
会变成:
string[] text = System.IO.File.ReadAllLines(dataFile.....;
foreach(int i =0; i<text.Length; i++)
{
replaceWordInLine(ref text[i] , disconnectors, word, wordBegining);
}
你可以开始了