Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

tuples

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 );
}   
like image 688
Vlad Avatar asked Dec 12 '22 02:12

Vlad


1 Answers

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);
like image 135
Lee Avatar answered Jan 18 '23 22:01

Lee