4

I am very new to the language. This code is giving me error:

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

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

I don't know what I am doing wrong. Can some tell me what is wrong

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 );
}   
Vlad
  • 279
  • 1
  • 3
  • 12
  • Try `new Tuple(sb.ToString(), lineNumber)` – John Saunders Oct 27 '14 at 23:25
  • Is this `while( (nc == rdr.Read() !=-1 ))` supposed to be this `while ((nc = rdr.Read()) != -1)` maybe? – SimpleVar Oct 28 '14 at 00:55
  • Instead of using the .NET Framework types `Int32` and `Char` to declare your variables, consider using the C# language types `int` and `char` instead. To learn why, see [int or Int32? Should I care?](http://stackoverflow.com/q/62503/1497596) and [What's the difference between String and string?](http://stackoverflow.com/q/7074/1497596). – DavidRR Nov 08 '14 at 01:24

1 Answers1

14

Tuple classes require type arguments which you must provide:

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

alternatively you can use Tuple.Create which usually allows the type arguments to be inferred automatically:

yield return Tuple.Create(sb.ToString(), lineNumber);
Lee
  • 142,018
  • 20
  • 234
  • 287
  • See [MSDN document](http://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx#code-snippet-2) code snippets 2 & 3 – Adam Smith Oct 27 '14 at 23:30