c# - 无法创建静态类的实例 'System.Tuple'

标签 c# tuples

我对这门语言很陌生。此代码给我错误:

Cannot create an instance of the static class 'System.Tuple'

Operator '!=' cannot be applied to operands of type 'bool' and 'int'

我不知道我做错了什么。谁能告诉我哪里出了问题

using(StreamReader rdr = new StreamReader("fileName"))
{
    StringBuilder sb = new StringBuilder();
    Int32 nc = 0;
    Char c;
    Int32 lineNumber = 0;
    while( (nc == rdr.Read() !=-1 ))
    {
        c = (Char)nc;
        if( Char.IsWhiteSpace(c) )
        {
            if( sb.Length > 0 )
            {
                yield return new Tuple( sb.ToString(), lineNumber );
                sb.Length = 0;
            }

            if( c == '\n' ) lineNumber++;
        } 
        else 
        {
            sb.Append( c );
        }
    }
    if( sb.Length > 0 ) yield return new Tuple( sb.ToString(), lineNumber );
}   

最佳答案

元组类需要您必须提供的类型参数:

yield return new Tuple<string, int>( sb.ToString(), lineNumber );

或者,您可以使用 Tuple.Create,它通常允许自动推断类型参数:

yield return Tuple.Create(sb.ToString(), lineNumber);

关于c# - 无法创建静态类的实例 'System.Tuple',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26598507/

相关文章:

java - 四元组零距离问题

javascript - C# MVC 发布模型和从 js 到 Controller 的附加数据

c# - .NET 中的消息代理服务器 (CoreCLR/DNX)

python - 从元组中删除元素

python - 为相同的字典值创建可交换元组键的最佳方法是什么?

erlang - 在 Erlang 元组上高效执行操作

python - 多维数组到多维元组

c# - 如何使用 MVC Web API 进行身份验证?

c# - 具有多个条件和组合的 LINQ 方法

c# - 监视应用程序一段时间后停止接收 UI 自动化事件,然后在一段时间后重新启动