Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

SByte.ToString Method

Converts the numeric value of this instance to its equivalent string representation.

The SByte type is not CLS-compliant. The CLS-compliant alternative type is Int16. For more information about CLS compliance, see What is the Common Language Specification.

Overload List

Converts the numeric value of this instance to its equivalent string representation.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function ToString() As String
[C#] public override string ToString();
[C++] public: String* ToString();
[JScript] public override function ToString() : String;

Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ToString(IFormatProvider) As String
[C#] public virtual string ToString(IFormatProvider);
[C++] public: virtual String* ToString(IFormatProvider*);
[JScript] public function ToString(IFormatProvider) : String;

Converts the numeric value of this instance to its equivalent string representation, using the specified format.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ToString(String) As String
[C#] public string ToString(string);
[C++] public: String* ToString(String*);
[JScript] public function ToString(String) : String;

Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ToString(String, IFormatProvider) As String
[C#] public virtual string ToString(string, IFormatProvider);
[C++] public: virtual String* ToString(String*, IFormatProvider*);
[JScript] public function ToString(String, IFormatProvider) : String;

Example

[Visual Basic, C#, C++] The following code example formats SByte (signed byte) values with several overloads of the ToString method.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ToString. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Example for the SByte.ToString( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module SByteToStringDemo
    
    Sub RunToStringDemo( )

        Dim smallValue As SByte = Convert.ToSByte( -99 )
        Dim largeValue As SByte = Convert.ToSByte( 123 )
            
        ' Format the SByte values without and with format strings.
        Console.WriteLine( vbCrLf & "IFormatProvider is not used:" )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "No format string:", smallValue.ToString( ), _
            largeValue.ToString( ) )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "'X2' format string:", smallValue.ToString( "X2" ), _
            largeValue.ToString( "X2" ) )
            
        ' Get the NumberFormatInfo object from the 
        ' invariant culture.
        Dim culture As New CultureInfo( "" )
        Dim numInfo As NumberFormatInfo = culture.NumberFormat
            
        ' Set decimal digits to 0. Set the negative pattern to ( ).
        numInfo.NumberNegativePattern = 0
        numInfo.NumberDecimalDigits = 0
            
        ' Use the NumberFormatInfo object for an IFormatProvider.
        Console.WriteLine( vbCrLf & _
            "A NumberFormatInfo object with negative pattern " & _
            "= ( ) and " & vbCrLf & "no decimal digits " & _
            "is used for the IFormatProvider:" )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "No format string:", smallValue.ToString( numInfo ), _
            largeValue.ToString( numInfo ) )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "'N' format string:", _
            smallValue.ToString( "N", numInfo ), _
            largeValue.ToString( "N", numInfo ) )
    End Sub 
        
    Sub Main( )

        Console.WriteLine( "This example of" & vbCrLf & _
            "   SByte.ToString( )," & vbCrLf & _ 
            "   SByte.ToString( String )," & vbCrLf & _
            "   SByte.ToString( IFormatProvider ), and" & vbCrLf & _
            "   SByte.ToString( String, IFormatProvider )" & _
            vbCrLf & "generates the following output when " & _
            "formatting SByte values " & vbCrLf & _
            "with combinations of format " & _
            "strings and IFormatProvider." )

        RunToStringDemo( )

    End Sub 
End Module 

' This example of
'    SByte.ToString( ),
'    SByte.ToString( String ),
'    SByte.ToString( IFormatProvider ), and
'    SByte.ToString( String, IFormatProvider )
' generates the following output when formatting SByte values
' with combinations of format strings and IFormatProvider.
'
' IFormatProvider is not used:
'    No format string:          -99       123
'    'X2' format string:         9D        7B
'
' A NumberFormatInfo object with negative pattern = ( ) and
' no decimal digits is used for the IFormatProvider:
'    No format string:          -99       123
'    'N' format string:        (99)       123

[C#] 
// Example for the SByte.ToString( ) methods.
using System;
using System.Globalization;

public class SByteToStringDemo  
{
    static void RunToStringDemo( )
    {    
        SByte smallValue = -99;
        SByte largeValue = 123;

        // Format the SByte values without and with format strings.
        Console.WriteLine( "\nIFormatProvider is not used:" );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "No format string:", smallValue.ToString( ), 
            largeValue.ToString( ) );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "'X2' format string:", smallValue.ToString( "X2" ), 
            largeValue.ToString( "X2" ) );

        // Get the NumberFormatInfo object from the 
        // invariant culture.
        CultureInfo         culture = new CultureInfo( "" );
        NumberFormatInfo    numInfo = culture.NumberFormat;

        // Set decimal digits to 0. Set the negative pattern to ( ).
        numInfo.NumberDecimalDigits = 0;
        numInfo.NumberNegativePattern = 0;

        // Use the NumberFormatInfo object for an IFormatProvider.
        Console.WriteLine( "\nA NumberFormatInfo " +
            "object with negative pattern = ( ) and \nno " +
            "decimal digits is used for the IFormatProvider:" );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "No format string:", smallValue.ToString( numInfo ), 
            largeValue.ToString( numInfo ) );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "'N' format string:", 
            smallValue.ToString( "N", numInfo ), 
            largeValue.ToString( "N", numInfo ) );
    }

    static void Main( )
    {    
        Console.WriteLine( 
            "This example of\n   SByte.ToString( ),\n" +
            "   SByte.ToString( string ),\n" +
            "   SByte.ToString( IFormatProvider ), and\n" +
            "   SByte.ToString( string, IFormatProvider )\n" +
            "generates the following output when formatting " +
            "SByte values \nwith combinations of format " +
            "strings and IFormatProvider." );
        
        RunToStringDemo( );
    } 
} 

/*
This example of
   SByte.ToString( ),
   SByte.ToString( string ),
   SByte.ToString( IFormatProvider ), and
   SByte.ToString( string, IFormatProvider )
generates the following output when formatting SByte values
with combinations of format strings and IFormatProvider.

IFormatProvider is not used:
   No format string:          -99       123
   'X2' format string:         9D        7B

A NumberFormatInfo object with negative pattern = ( ) and
no decimal digits is used for the IFormatProvider:
   No format string:          -99       123
   'N' format string:        (99)       123
*/

[C++] 
// Example for the SByte::ToString( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

void RunToStringDemo( )
{    
    SByte smallValue = -99;
    SByte largeValue = 123;

    // Format the Byte values without and with format strings.
    Console::WriteLine( S"\nIFormatProvider is not used:" );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"No format string:", smallValue.ToString( ), 
        largeValue.ToString( ) );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"'X2' format string:", smallValue.ToString( S"X2" ), 
        largeValue.ToString( S"X2" ) );

    // Get the NumberFormatInfo object from the invariant culture.
    CultureInfo*        culture = new CultureInfo( S"" );
    NumberFormatInfo*   numInfo = culture->NumberFormat;

    // Set decimal digits to 0. Set the negative pattern to ( ).
    numInfo->NumberDecimalDigits = 0;
    numInfo->NumberNegativePattern = 0;

    // Use the NumberFormatInfo object for an IFormatProvider.
    Console::WriteLine( S"\nA NumberFormatInfo " 
        S"object with negative pattern = ( ) and \nno " 
        S"decimal digits is used for the IFormatProvider:" );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"No format string:", smallValue.ToString( numInfo ), 
        largeValue.ToString( numInfo ) );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"'N' format string:", 
        smallValue.ToString( S"N", numInfo ), 
        largeValue.ToString( S"N", numInfo ) );
} 

void main( )
{    
    Console::WriteLine( 
        S"This example of\n   SByte::ToString( ),\n" 
        S"   SByte::ToString( String* ),\n" 
        S"   SByte::ToString( IFormatProvider* ), and\n" 
        S"   SByte::ToString( String*, IFormatProvider* )\n" 
        S"generates the following output when formatting " 
        S"SByte values \nwith combinations of format " 
        S"strings and IFormatProvider." );
    
    RunToStringDemo( );
}

/*
This example of
   SByte::ToString( ),
   SByte::ToString( String* ),
   SByte::ToString( IFormatProvider* ), and
   SByte::ToString( String*, IFormatProvider* )
generates the following output when formatting SByte values
with combinations of format strings and IFormatProvider.

IFormatProvider is not used:
   No format string:          -99       123
   'X2' format string:         9D        7B

A NumberFormatInfo object with negative pattern = ( ) and
no decimal digits is used for the IFormatProvider:
   No format string:          -99       123
   'N' format string:        (99)       123
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

SByte Structure | SByte Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.