locked
Difference between StringWriter and StreamWriter in C#? RRS feed

  • Question

  • What is the difference between StringWriter and StreamWriter in C#?
    Thursday, January 15, 2009 7:43 PM

Answers

  • From the Object Browser Window....

    public class StringWriter : System.IO.TextWriter  
        Member of System.IO  
     
    Summary:  
    Implements a System.IO.TextWriter for writing information to a string.   
    The information is stored in an underlying System.Text.StringBuilder. 

    public class StreamWriter : System.IO.TextWriter  
        Member of System.IO  
     
    Summary:  
    Implements a System.IO.TextWriter for writing   
    characters to a stream in a particular encoding. 

    One writes to a concrete type, string.  The other writes to an abstract type, stream, which can represent a file, a buffer, memory, etc.  The abstract type can even perform specific formatting unique to its class.


    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Harry Zhu Monday, January 19, 2009 8:48 AM
    • Marked as answer by Harry Zhu Wednesday, January 21, 2009 11:37 AM
    Thursday, January 15, 2009 8:20 PM
  •  There is a good example at this MSDN site that illustrates how StringWrter works. Let's say you have a text stored in a string variable. it can be a page of Shakespear's Hamlet for instance. Perhaps you obtained it by reading some Sql Server table column. You want to analyze this string/text for some meaning, perhaps count the number of lines, etc. You use this class: StreingReader which is also illustrated in there and then use StringWriter to write something back to a file after you did some analysis. Take a look at the code.

    StreamWriter is more universal. It can handle binary code, ASCII characters, Unicode, etc. StringReader is more specialized.

    AlexB
    • Proposed as answer by Harry Zhu Monday, January 19, 2009 8:48 AM
    • Marked as answer by Harry Zhu Wednesday, January 21, 2009 11:37 AM
    Thursday, January 15, 2009 8:22 PM

All replies

  • From the Object Browser Window....

    public class StringWriter : System.IO.TextWriter  
        Member of System.IO  
     
    Summary:  
    Implements a System.IO.TextWriter for writing information to a string.   
    The information is stored in an underlying System.Text.StringBuilder. 

    public class StreamWriter : System.IO.TextWriter  
        Member of System.IO  
     
    Summary:  
    Implements a System.IO.TextWriter for writing   
    characters to a stream in a particular encoding. 

    One writes to a concrete type, string.  The other writes to an abstract type, stream, which can represent a file, a buffer, memory, etc.  The abstract type can even perform specific formatting unique to its class.


    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Harry Zhu Monday, January 19, 2009 8:48 AM
    • Marked as answer by Harry Zhu Wednesday, January 21, 2009 11:37 AM
    Thursday, January 15, 2009 8:20 PM
  •  There is a good example at this MSDN site that illustrates how StringWrter works. Let's say you have a text stored in a string variable. it can be a page of Shakespear's Hamlet for instance. Perhaps you obtained it by reading some Sql Server table column. You want to analyze this string/text for some meaning, perhaps count the number of lines, etc. You use this class: StreingReader which is also illustrated in there and then use StringWriter to write something back to a file after you did some analysis. Take a look at the code.

    StreamWriter is more universal. It can handle binary code, ASCII characters, Unicode, etc. StringReader is more specialized.

    AlexB
    • Proposed as answer by Harry Zhu Monday, January 19, 2009 8:48 AM
    • Marked as answer by Harry Zhu Wednesday, January 21, 2009 11:37 AM
    Thursday, January 15, 2009 8:22 PM