sbyte (C# Reference)

sbyte denotes an integral type that stores values according to the size and range shown in the following table.

Type Range Size .NET Framework type
sbyte -128 to 127 Signed 8-bit integer System.SByte

Literals

You can declare and initialize an sbyte variable by assigning a decimal literal, a hexadecimal literal, or (starting with C# 7) a binary literal to it.

In the following example, integers equal to -102 that are represented as decimal, hexadecimal, and binary literals are converted from int to sbyte values.

sbyte sbyteValue1 = -102;
Console.WriteLine(sbyteValue1);

unchecked {
   sbyte sbyteValue4 = (sbyte)0x9A;
   Console.WriteLine(sbyteValue4);
   
   sbyte sbyteValue5 = (sbyte)0b1001_1010;
   Console.WriteLine(sbyteValue5);
}
// The example displays the following output:
//          -102
//          -102
//          -102
Note

You use the prefix 0x or 0X to denote a hexadecimal literal and the prefix 0b or 0B to denote a binary literal. Decimal literals have no prefix.

Starting with C# 7, you can also use the underscore character, _, as a digit separator to enhance readability, as the following example shows.

unchecked {
   sbyte sbyteValue3 = (sbyte)0b1001_1010;
   Console.WriteLine(sbyteValue3);
}
// The example displays the following output:
//          -102

If the integer literal is outside the range of sbyte (that is, if it is less than System.SByte.MinValue or greater than System.SByte.MaxValue, a compilation error occurs. When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong. This means that, in this example, the numeric literals 0x9A and 0b10011010 are interpreted as 32-bit signed integers with a value of 156, which exceeds System.SByte.MaxValue. Because of this, the casting operator is needed, and the assignment must occur in an unchecked context.

Compiler overload resolution

A cast must be used when calling overloaded methods. Consider, for example, the following overloaded methods that use sbyte and int parameters:

public static void SampleMethod(int i) {}  
public static void SampleMethod(sbyte b) {}  

Using the sbyte cast guarantees that the correct type is called, for example:

// Calling the method with the int parameter:  
SampleMethod(5);  
// Calling the method with the sbyte parameter:  
SampleMethod((sbyte)5);  

Conversions

There is a predefined implicit conversion from sbyte to short, int, long, float, double, or decimal.

You cannot implicitly convert nonliteral numeric types of larger storage size to sbyte (see Integral Types Table for the storage sizes of integral types). Consider, for example, the following two sbyte variables x and y:

sbyte x = 10, y = 20;  

The following assignment statement will produce a compilation error, because the arithmetic expression on the right side of the assignment operator evaluates to int by default.

sbyte z = x + y;   // Error: conversion from int to sbyte  

To fix this problem, cast the expression as in the following example:

sbyte z = (sbyte)(x + y);   // OK: explicit conversion  

It is possible though to use the following statements, where the destination variable has the same storage size or a larger storage size:

sbyte x = 10, y = 20;  
int m = x + y;  
long n = x + y;  

Notice also that there is no implicit conversion from floating-point types to sbyte. For example, the following statement generates a compiler error unless an explicit cast is used:

sbyte x = 3.0;         // Error: no implicit conversion from double  
sbyte y = (sbyte)3.0;  // OK: explicit conversion  

For information about arithmetic expressions with mixed floating-point types and integral types, see float and double.

For more information about implicit numeric conversion rules, see the Implicit Numeric Conversions Table.

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See Also

SByte
C# Reference
C# Programming Guide
C# Keywords
Integral Types Table
Built-In Types Table
Implicit Numeric Conversions Table
Explicit Numeric Conversions Table