Open In App

C# | Convert.ToString(String, IFormatProvider) Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

This method is used to returns the specified string instance and no actual conversion is performed.
Syntax:

public static string ToString (String, IFormatProvider);

Parameters:  

  • value: It is the string to return.
  • provider: It is an object that supplies culture-specific formatting information. This parameter is ignored.

Return Value: This method returns value is returned unchanged.
Below programs illustrate the use of Convert.ToString(String, IFormatProvider) Method:
Example 1: 

csharp




// C# program to demonstrate the
// Convert.ToString() Method
using System;
using System.Globalization;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        try {
 
            // creating object of CultureInfo
            CultureInfo cultures =
              new CultureInfo("en-US");
 
            // declaring and initializing String array
            string[] values = {"amar", "akbar",
                                    "anthony"};
 
            // calling get() Method
            Console.Write("Converted string value"
                  + " from a specified string: ");
 
            for (int j = 0; j < values.Length; j++)
            {
                get(values[j], cultures);
            }
        }
 
        catch (FormatException e)
        {
            Console.WriteLine("\n");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
 
        catch (OverflowException e)
        {
            Console.WriteLine("\n");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
 
    // Defining get() method
    public static void get(string s,
               CultureInfo cultures)
    {
 
        // converting string to specified string
        string val = Convert.ToString(s, cultures);
 
        // display the converted string value
        Console.Write(" {0} ", val);
    }
}


Output: 

Converted string value from a specified string:  amar  akbar  anthony

 

Example 2:

csharp




// C# program to demonstrate the
// Convert.ToString() Method
using System;
using System.Globalization;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        // creating object of CultureInfo
        CultureInfo cultures =
          new CultureInfo("en-US");
 
        // declaring and initializing a String array
        string value = "The Accidental Prime Minister";
 
        // calling get() Method
        Console.Write("Converted String: ");
 
        // converting string to specified string
        string val = Convert.ToString(value, cultures);
 
        // display the converted string value
        Console.Write("{0}", val);
    }
}


Output: 

Converted String: The Accidental Prime Minister

 

Reference: 



Last Updated : 02 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads