System.IO.StringReader Class

Assembly: Mscorlib.dll
Namespace: System.IO
Summary
Implements a TextReader that reads from a string.
C# Syntax:
[Serializable]
public class StringReader : TextReader
See also:
System.IO Namespace | TextReader | StringWriter

System.IO.StringReader Member List:

Public Constructors
ctor #1 Initializes a new instance of the StringReader class that reads from the specified string.
Public Methods
Close Overridden:
Closes the StringReader.
CreateObjRef
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.CreateObjRef


Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.GetLifetimeService


Retrieves the current lifetime service object that controls the lifetime policy for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
InitializeLifetimeService
(inherited from System.MarshalByRefObject)
See base class member description: System.MarshalByRefObject.InitializeLifetimeService


Obtains a lifetime service object to control the lifetime policy for this instance.
Peek Overridden:
Returns the next available character but does not consume it.
Read Overloaded:
Read()

Overridden:
Reads the next character from the input string and advances the character position by one character.
Read Overloaded:
Read(in char[] buffer, int index, int count)

Overridden:
Reads a block of characters from the input string and advances the character position by count.
ReadBlock
(inherited from System.IO.TextReader)
See base class member description: System.IO.TextReader.ReadBlock


Reads a maximum of count characters from the current stream and writes the data to buffer, beginning at index.
ReadLine Overridden:
Reads a line from the underlying string.
ReadToEnd Overridden:
Reads the stream as a string, either in its entirety or from the current position to the end of the stream.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Dispose Overridden:
Releases the unmanaged resources used by the StringReader and optionally releases the managed resources.
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.IO.StringReader Member Details

ctor #1
Summary
Initializes a new instance of the StringReader class that reads from the specified string.
C# Syntax:
public StringReader(
   string s
);
Parameters:

s

The string to which the StringReader should be initialized.

Exceptions
Exception Type Condition
ArgumentNullException The s parameter is null.

Return to top


Overridden Method: Close()
Summary
Closes the StringReader.
C# Syntax:
public override void Close();
Remarks
This method overrides the Stream.Close method.

This implementation of Close calls the StringReader.Dispose method passing a true value.

Flushing the stream will not flush its underlying encoder unless you explicitly call Close. Setting StreamWriter.AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

Following a call to Close, other methods might throw an exception.

Return to top


Method: CreateObjRef(
   Type requestedType
)
Inherited
See base class member description: System.MarshalByRefObject.CreateObjRef

Summary
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
C# Syntax:
public virtual ObjRef CreateObjRef(
   Type requestedType
);
Parameters:

requestedType

The Type of the object that the new ObjRef will reference.

Return Value:
Information required to generate a proxy.
Exceptions
Exception Type Condition
RemotingException This instance is not a valid remoting object.

Return to top


Overridden Method: Dispose(
   bool disposing
)
Summary
Releases the unmanaged resources used by the StringReader and optionally releases the managed resources.
C# Syntax:
protected override void Dispose(
   bool disposing
);
Parameters:

disposing

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Remarks
When the disposing parameter is true, this method releases all resources held by any managed objects that this StringReader references. This method invokes the Dispose() method of each referenced object.

Notes to inheritors: StringReader.Dispose may be called multiple times by other objects. When overriding StringReader.Dispose, be careful not to reference objects that have been previously disposed in an earlier call to StringReader.Dispose.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~StringReader();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.GetLifetimeService

Summary
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
C# Syntax:
public object GetLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance.
Remarks
For more information about lifetime services, see the LifetimeServices class.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: InitializeLifetimeService()
Inherited
See base class member description: System.MarshalByRefObject.InitializeLifetimeService

Summary
Obtains a lifetime service object to control the lifetime policy for this instance.
C# Syntax:
public virtual object InitializeLifetimeService();
Return Value:
An object of type ILease used to control the lifetime policy for this instance. This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the LifetimeServices.LeaseManagerPollTime property.
Remarks
For more information about lifetime services, see the LifetimeServices class.
Example
The following code example demonstrates creating a lease.
 public class MyClass : MarshalByRefObject
 {
   public override Object InitializeLifetimeService()
   {
     ILease lease = (ILease)base.InitializeLifetimeService();
     if (lease.CurrentState == LeaseState.Initial)
     {
          lease.InitialLeaseTime = TimeSpan.FromMinutes(1);
          lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
           lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
     }
       return lease;
   }
 }

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: Peek()
Summary
Returns the next available character but does not consume it.
C# Syntax:
public override int Peek();
Return Value:
The next character to be read, or -1 if no more characters are available or the stream does not support seeking.
Exceptions
Exception Type Condition
ObjectDisposedException The current reader is closed.
Remarks
This method overrides the TextReader.Peek method.

The current position of the StringReader is not changed by this operation.

Return to top


Overloaded Method: Read()
Summary
Reads the next character from the input string and advances the character position by one character.
C# Syntax:
public override int Read();
Return Value:
The next character from the underlying string, or -1 if no more characters are available.
Exceptions
Exception Type Condition
ObjectDisposedException The current reader is closed.
Remarks
This method overrides the TextReader.Read method.

Return to top


Overloaded Method: Read(
   in char[] buffer,
   int index,
   int count
)
Summary
Reads a block of characters from the input string and advances the character position by count.
C# Syntax:
public override int Read(
   in char[] buffer,
   int index,
   int count
);
Parameters:

buffer

When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

index

The starting index in the buffer.

count

The number of characters to read.

Return Value:
The total number of characters read into the buffer. This may be less than the number of characters requested if that many characters are not currently available, or zero if the end of the underlying string has been reached.
Exceptions
Exception Type Condition
ArgumentNullException buffer is null.
ArgumentException The buffer length minus index is less than count.
ArgumentOutOfRangeException index or count is negative.
ObjectDisposedException The current reader is closed.
Remarks
This method overrides TextReader.Read.

The method will read up to count characters from the StringReader into the buffer character array starting at position index. Returns the actual number of characters read, or zero if the end of the string has been reached and no characters are read.

Return to top


Method: ReadBlock(
   in char[] buffer,
   int index,
   int count
)
Inherited
See base class member description: System.IO.TextReader.ReadBlock

Summary
Reads a maximum of count characters from the current stream and writes the data to buffer, beginning at index.
C# Syntax:
public virtual int ReadBlock(
   in char[] buffer,
   int index,
   int count
);
Parameters:

buffer

When this method returns, contains the specified character array with the values between index and(index + count) replaced by the characters read from the current source.

index

The place in buffer at which to begin writing.

count

The maximum number of characters to read.

Return Value:
The number of characters that have been read. The number will be less than or equal to count, depending on whether all input characters have been read.
Exceptions
Exception Type Condition
ArgumentNullException buffer is null.
ArgumentException The buffer length minus index is less than count.
ArgumentOutOfRangeException index or count is negative.
IOException An I/O error occurs.
Remarks
The method blocks until either count characters are read, or all characters have been read. This is a blocking version of TextReader.Read.

Return to top


Overridden Method: ReadLine()
Summary
Reads a line from the underlying string.
C# Syntax:
public override string ReadLine();
Return Value:
The next line from the underlying string, or null if the end of the underlying string is reached.
Exceptions
Exception Type Condition
ObjectDisposedException The current reader is closed.
OutOfMemoryException There is insufficient memory to allocate a buffer for the returned string.
Remarks
This method overrides the TextReader.ReadLine method.

A line is defined as a sequence of characters followed by a carriage return ("\r"), a line feed ("\n"), or a carriage return immediately followed by a line feed. The resulting string does not contain the terminating carriage return and/or line feed. The returned value is null if the end of the underlying string has been reached.

If the current method throws an OutOfMemoryException, the reader's position in the underlying string is advanced by the number of characters the method was able to read, and the characters already read into the internal StringReader.ReadLine buffer are discarded. Since the position of the reader in the string cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StringReader. To avoid such a situation and produce robust code you should use the StringReader.Read method and store the read characters in a preallocated buffer.

Return to top


Overridden Method: ReadToEnd()
Summary
Reads the stream as a string, either in its entirety or from the current position to the end of the stream.
C# Syntax:
public override string ReadToEnd();
Return Value:
The content from the current position to the end of the underlying string.
Exceptions
Exception Type Condition
OutOfMemoryException There is insufficient memory to allocate a buffer for the returned string.
ObjectDisposedException The current reader is closed.
Remarks
This method overrides the TextReader.ReadToEnd method.

If the current method throws an OutOfMemoryException, the reader's position in the underlying string is advanced by the number of characters the method was able to read, and the characters already read into the internal StringReader.ReadToEnd buffer are discarded. Since the position of the reader in the string cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StringReader. To avoid such a situation and produce robust code you should use the StringReader.Read method and store the read characters in a preallocated buffer.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.