【问题标题】:Passing paramerets are showing wrong for a Custom Method自定义方法的传递参数显示错误
【发布时间】:2014-09-15 15:57:01
【问题描述】:

我有一个方法,我需要调用我的一个方法并使用从该方法返回的字符串数组,但我传递的参数不正确。

这是我在我的方法中调用的方法..

public static string[] ReadFromFile(string filePath, int count, ref int lineCount)
{
     lineCount += count;
     return File.ReadLines(filePath).Skip(lineCount).Take(count).ToArray();
}

这是我传递的参数..

string filepath="C://Data";
int count;
int lineCount;

string[] arrline= ReadFromFile(filepath,count,lineCount);

请帮助我哪里错了。 谢谢..

【问题讨论】:

  • 所有变量都是字符串,即使你想将它们用作计数器。
  • @TimSchmelter 对不起,他们是 int 。我更新了
  • 现在你传递零作为计数,这意味着你想要Take(0) 行。

标签: c# methods parameters arguments


【解决方案1】:
string[] arrline= ReadFromFile(filepath,count,ref lineCount);

传递的参数中缺少 ref 关键字

【讨论】:

  • ref 是什么意思。请详细说明。谢谢
  • 这意味着您传递的是对整数的引用,而不是实际的整数值。 msdn.microsoft.com/en-gb/library/14akc2c7.aspx 在这种情况下存在,因为您需要从该方法返回两个不同的值。您对方法中的值所做的任何更改都将通过您声明已传递给方法的变量提供。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 2013-10-21
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 2023-01-13
相关资源
最近更新 更多