C# | SByte Struct Fields
Last Updated :
02 May, 2019
Improve
In C#, Sbyte Struct comes under the System namespace which represents an 8-bit signed integer. The SByte value type represents integers with values ranging from -128 to +127. There are the two fields in the System.SByte Struct as follows:
CSharp
Output:
CSharp
Output:
- SByte.MaxValue Field
- SByte.MinValue Field
SByte.MaxValue Field
This is a constant field which represents the largest possible value(127) of SByte. Syntax:public const sbyte MaxValue = 127;Example:
// C# program to demonstrate the SByte.MaxValue
// Field by checking whether the given +ve long
// value can be converted to sbyte value or not
using System;
class Max_Geeks {
// Main method
static public void Main()
{
// Only taking +ve values
long lValue = 128;
sbyte sbValue;
// Using the MaxValue Field to check
// whether the conversion is Possible
// or not for +ve values only
if (lValue <= sbyte.MaxValue) {
// Type conversion from long to sbyte
sbValue = (sbyte)lValue;
Console.WriteLine("Converted long integer value to {0}.", sbValue);
}
else {
Console.WriteLine("Conversion is not Possible");
}
}
}
Conversion is not Possible
SByte.MinValue Field
This is a constant field which represents the smallest possible value(-128) of SByte. Syntax:public const sbyte MinValue = -128;Example:
// C# program to demonstrate the SByte.Min Value
// Field by checking whether the given -ve long
// value can be converted to sbyte value or not
using System;
class Min_Geeks {
// Main method
static public void Main()
{
// Only taking -ve values
long lValue = -128;
sbyte sbValue;
// Using the MinValue Field to check
// whether the conversion is Possible
// or not for -ve values only
if (lValue >= sbyte.MinValue) {
// Type conversion from long to sbyte
sbValue = (sbyte)lValue;
Console.WriteLine("Converted long integer value to {0}", sbValue);
}
else {
Console.WriteLine("Conversion is not Possible");
}
}
}
Converted long integer value to -128References:
- https://docs.microsoft.com/en-us/dotnet/api/system.sbyte.maxvalue?view=netframework-4.7.2
- https://docs.microsoft.com/en-us/dotnet/api/system.sbyte.minvalue?view=netframework-4.7.2