📜  C#| SByte结构字段

📅  最后修改于: 2021-05-29 18:31:33             🧑  作者: Mango

在C#中, Sbyte Struct位于System命名空间下,该命名空间表示8位带符号整数。 SByte值类型表示值在-128到+127之间的整数。 System.SByte结构中有两个字段,如下所示:

  1. SByte.MaxValue字段
  2. SByte.MinValue字段

SByte.MaxValue字段

这是一个常量字段,代表SByte的最大可能值(127)。

句法:

public const sbyte MaxValue = 127;

例子:

// 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字段

这是一个常量字段,代表SByte的最小可能值(-128)。

句法:

public const sbyte MinValue = -128;

例子:

// 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 -128

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.sbyte.maxvalue?view=netframework-4.7.2
  • https://docs.microsoft.com/zh-cn/dotnet/api/system.sbyte.minvalue?view=netframework-4.7.2