Alphabetize using I/O in C# -
i'm trying use i/o , sort input file alphabetically using method called alphabetize, has compile error
(35,42): error cs1061: 'string' not contain definition 'toarray' , no extension method 'toarray' accepting first argument of type 'string' found (are missing using directive or assembly reference?)
what can resolve that?
using system; using system.io; namespace examplefile { class examplefile { static void main(string[] args) { streamwriter writer = null; writer = new streamwriter(@"c:\c#files\outputwrite2.txt"); console.setout(writer); console.setin(new streamreader(@"c:\c#files\inputread2.txt")); string letters; while ((letters = console.readline()) != null) writer.close(); streamwriter standardoutput = new streamwriter(console.openstandardoutput()); standardoutput.autoflush = true; console.setout(standardoutput); console.writeline("sorted letters alphabetically , wrote output file."); } public static string alphabetize(string letters) { char[] alphabetize = letters.toarray(); array.sort(alphabetize); return new string(alphabetize); } } }
your problem here:
char[] alphabetize = letters.toarray();
as error got states:
'string' not contain definition 'toarray' , no extension method 'toarray' accepting first argument of type 'string' found
there 2 parts error message. first part says string
doesn't have toarray()
method. since letters
string
, can't call toarray()
on it. try instead:
char[] alphabetize = letters.tochararray();
the second part of error tells there no toarray
extension method defined in context; however, system.linq
namespace has extension method toarray<tsource>()
on interface ienumerable<char>
. string
implements ienumerable<char>
, if include system.linq
namespace in file, can call extension method toarray<tsource>()
:
// @ top of file other includes. using system.text; // can this: char[] alphabetize = letters.toarray();
Comments
Post a Comment