Within this guide, you will be introduced to the Convert.ToSByte(String, IFormatProvider) function in C#, along with its syntax, input parameters, and illustrative instances.
What is Convert.ToSByte(String, IFormatProvider) method?
In C#, the Convert.ToSByte(String, IFormatProvider) function changes the provided string that represents a number into a corresponding signed byte (SByte), with the specified format provider.
Syntax:
It has the following syntax:
public static sbyte ToSByte(string value, IFormatProvider provider);
Parameters:
value: A string containing the number to convert.
provider: This is an IFormatProvider that provides culture-specific formatting details. It can be left as null to utilize the default format provider.
Return Value:
Following the conversion, the operation yields a signed byte (sbyte).
Exceptions:
It is crucial to appropriately manage exceptions that may be raised during the execution of a procedure. Common exceptions include:
- : This exception occurs when there is an issue with the format of a string, referred to as a FormatException.
- : When a conversion leads to an overflow, an OverflowException is triggered.
Example:
Let's consider a scenario to demonstrate the Convert.ToSByte(String, IFormatProvider) function in C#.
using System;
using System.Globalization;
class Demo
{
static void Main()
{
// Example 1: Valid conversion
PerformingSByteConversion("123");
// Example 2: Invalid format
PerformingSByteConversion("abc");
// Example 3: Overflow occurred
PerformingSByteConversion("256");
// Example 4: Using a custom format provider
IFormatProvider customFormatProvider = new CultureInfo("fr-FR"); // French culture
PerformingSByteConversion("123,45", customFormatProvider);
}
static void PerformingSByteConversion(string value, IFormatProvider provider = null)
{
Console.WriteLine($"Attempting to convert '{value}' to SByte.");
try
{
sbyte result_value = Convert.ToSByte(value, provider);
Console.WriteLine($"Conversion successful, and the Result is {result_value}");
}
catch (FormatException)
{
Console.WriteLine("Invalid format for the conversion.");
}
catch (OverflowException)
{
Console.WriteLine("The overflow occurred during the conversion.");
}
catch (Exception e)
{
Console.WriteLine($"An unexpected exception occurred: {e.Message}");
}
Console.WriteLine();
}
}
Output:
Attempting to convert '123' to SByte.
Conversion successful, and the Result is 123
Attempting to convert 'abc' to SByte.
Invalid format for the conversion.
Attempting to convert '256' to SByte.
The overflow occurred during the conversion.
Attempting to convert '123,45' to SByte.
Invalid format for the conversion.
Explanation:
- Main Function: The Main function serves as the starting point of the software. It showcases four various approaches to utilizing the PerformingSByteConversion function for converting strings into sbytes.
An example of a legitimate transformation involving the string "123" is:
An unsuccessful conversion endeavor was made on the string "abc" due to an incorrect format.
Converting the value "256" leads to an overflow error.
Example 4: Converting the string "123,45" with a custom format provider (fr-FR - French culture).
- Performing the SbyteConversion Method: This method is responsible for carrying out the ToSByte functionality and handling possible exceptions.
- The method accepts a value and an optional provider parameter for a custom format provider.
- It prints a message indicating the attempt to convert the provided string.
- It attempts to convert the string into a sbyte i nside the try block.
- It prints the Result if the conversion is successful.
- If a FormatException occurs, it handles it by printing a message about an invalid format.
- If an OverflowException occurs, it handles it by printing a message about an overflow.
- The catch (Exception e) block is a generic catch-all for any unexpected exceptions, printing a message with the exception details.
- Each situation concludes with an empty line for better readability in the console output.
Format Exception:
Let's consider a scenario to demonstrate the format exception in the Convert.ToSByte(String, IFormatProvider) function within C#.
using System;
using System.Globalization;
class Demo
{
static void Main()
{
string String_Value = "pqr";
IFormatProvider customFormatProvider = new CultureInfo("en-US");
try
{
sbyte result_value = Convert.ToSByte(String_Value, customFormatProvider);
Console.WriteLine($"Converted value: {result_value}");
}
catch (FormatException e)
{
Console.WriteLine($"Format exception: {e.Message}");
}
}
}
Output:
Format exception: The input string was not in the correct format.
Explanation:
This code showcases the usage of "Convert.ToSByte" with a personalized format provider and managing a FormatException when the conversion operation encounters an issue. In this specific scenario, the string "pqr" is not convertible to a signed byte, leading to the occurrence of a FormatException. Subsequently, the error message related to the exception is displayed on the console.
Overflow Exception:
Let's consider a scenario to demonstrate the overflow error in the Convert.ToSByte(String, IFormatProvider) function within the C# programming language.
using System;
using System.Globalization;
class Demo
{
static void Main()
{
string string_value = "128";
try
{
sbyte result_value = Convert.ToSByte(string_value,CultureInfo.InvariantCulture);
Console.WriteLine("Conversion successful: " + result_value);
}
catch (OverflowException e)
{
Console.WriteLine("Overflow Exception: " + e.Message);
}
catch (FormatException e)
{
Console.WriteLine("Format Exception: " + e.Message);
}
catch (Exception e)
{
Console.WriteLine("An unexpected error occurred: " + e.Message);
}
}
}
Output:
Overflow Exception: The value was too large or too small for a signed byte.
Explanation:
In this instance, the method "Convert.ToSByte" is employed to try converting the string "128" into a signed byte. Nonetheless, the outcome exceeds the acceptable range for a signed byte, which spans from -128 to 127. Consequently, an OverflowException will be triggered, and the code will manage and address the exception appropriately.