C# Int16 Parse(String)

Description

Int16 Parse(String) converts the string representation of a number to its 16-bit signed integer equivalent.

Syntax

Int16.Parse(String) has the following syntax.


public static short Parse(
  string s
)

Parameters

Int16.Parse(String) has the following parameters.

  • s - A string containing a number to convert.

Returns

Int16.Parse(String) method returns A 16-bit signed integer equivalent to the number contained in s.

Example

The following example demonstrates how to convert a string value into a 16-bit signed integer value using the Int16.Parse(String) method.


//from  ww w.j  a va  2s  .co m
using System;
public class MainClass{
  public static void Main(String[] argv){  
    string value;
    short number;
    
    value = " 12345 ";
    try
    {
       number = Int16.Parse(value);
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    }
    catch (FormatException)
    {
       Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", 
                         value);
    }
    
    value = " 12,345";
    try
    {
       number = Int16.Parse(value);
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    }
    catch (FormatException)
    {
       Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", 
                         value);
    }
    
    value = " -12345";
    try
    {
       number = Int16.Parse(value);
       Console.WriteLine("Converted '{0}' to {1}.", value, number);
    }
    catch (FormatException)
    {
       Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", 
                         value);
    }

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version