Open In App

C# | SByte Struct Fields

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:



  1. SByte.MaxValue Field
  2. 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");
        }
    }
}

Output:

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");
        }
    }
}

Output:

Converted long integer value to -128

References:


Article Tags :
C#