Math.Sign Method (SByte)

Returns a value indicating the sign of an 8-bit signed integer.

This API is not CLS-compliant. 

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<CLSCompliantAttribute(False)> _
Public Shared Function Sign ( _
    value As SByte _
) As Integer
[CLSCompliantAttribute(false)]
public static int Sign(
    sbyte value
)

Parameters

Return Value

Type: System.Int32
A number indicating the sign of value.

Number

Description

-1

value is less than zero.

0

value is equal to zero.

1

value is greater than zero.

Examples

The following example demonstrates how to use the Sign method to determine the sign of a SByte value and print it to the console.

' This example demonstrates Math.Sign()
Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim str As String = "{0}: {1,3} is {2} zero."
      Dim nl As String = Environment.NewLine

      Dim xByte1 As Byte = 0
      Dim xShort1 As Short = -2
      Dim xInt1 As Integer = -3
      Dim xLong1 As Long = -4
      Dim xSingle1 As Single = 0.0F
      Dim xDouble1 As Double = 6.0
      Dim xDecimal1 As [Decimal] = -7D

      ' The following type is not CLS-compliant.
      Dim xSbyte1 As SByte = -101

      outputBlock.Text += String.Format("{0}Test the sign of the following types of values:", nl) & vbCrLf
      outputBlock.Text += String.Format(str, "Byte   ", xByte1, Test(Math.Sign(xByte1))) & vbCrLf
      outputBlock.Text += String.Format(str, "Int16  ", xShort1, Test(Math.Sign(xShort1))) & vbCrLf
      outputBlock.Text += String.Format(str, "Int32  ", xInt1, Test(Math.Sign(xInt1))) & vbCrLf
      outputBlock.Text += String.Format(str, "Int64  ", xLong1, Test(Math.Sign(xLong1))) & vbCrLf
      outputBlock.Text += String.Format(str, "Single ", xSingle1, Test(Math.Sign(xSingle1))) & vbCrLf
      outputBlock.Text += String.Format(str, "Double ", xDouble1, Test(Math.Sign(xDouble1))) & vbCrLf
      outputBlock.Text += String.Format(str, "Decimal", xDecimal1, Test(Math.Sign(xDecimal1))) & vbCrLf
      '
      outputBlock.Text += String.Format("{0}The following type is not CLS-compliant.", nl) & vbCrLf
      outputBlock.Text += String.Format(str, "SByte  ", xSbyte1, Test(Math.Sign(xSbyte1))) & vbCrLf
   End Sub 'Main
   '
   Public Shared Function Test(ByVal [compare] As Integer) As [String]
      If [compare] = 0 Then
         Return "equal to"
      ElseIf [compare] < 0 Then
         Return "less than"
      Else
         Return "greater than"
      End If
   End Function 'Test
End Class 'Sample 
'
'This example produces the following results:
'
'Test the sign of the following types of values:
'Byte   :   0 is equal to zero.
'Int16  :  -2 is less than zero.
'Int32  :  -3 is less than zero.
'Int64  :  -4 is less than zero.
'Single :   0 is equal to zero.
'Double :   6 is greater than zero.
'Decimal:  -7 is less than zero.
'
'The following type is not CLS-compliant.
'SByte  : -101 is less than zero.
// This example demonstrates Math.Sign()
using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string str = "{0}: {1,3} is {2} zero.";
      string nl = Environment.NewLine;

      byte xByte1 = 0;
      short xShort1 = -2;
      int xInt1 = -3;
      long xLong1 = -4;
      float xSingle1 = 0.0f;
      double xDouble1 = 6.0;
      Decimal xDecimal1 = -7m;

      // The following type is not CLS-compliant.
      sbyte xSbyte1 = -101;

      outputBlock.Text += String.Format("{0}Test the sign of the following types of values:", nl) + "\n";
      outputBlock.Text += String.Format(str, "Byte   ", xByte1, Test(Math.Sign(xByte1))) + "\n";
      outputBlock.Text += String.Format(str, "Int16  ", xShort1, Test(Math.Sign(xShort1))) + "\n";
      outputBlock.Text += String.Format(str, "Int32  ", xInt1, Test(Math.Sign(xInt1))) + "\n";
      outputBlock.Text += String.Format(str, "Int64  ", xLong1, Test(Math.Sign(xLong1))) + "\n";
      outputBlock.Text += String.Format(str, "Single ", xSingle1, Test(Math.Sign(xSingle1))) + "\n";
      outputBlock.Text += String.Format(str, "Double ", xDouble1, Test(Math.Sign(xDouble1))) + "\n";
      outputBlock.Text += String.Format(str, "Decimal", xDecimal1, Test(Math.Sign(xDecimal1))) + "\n";
      //
      outputBlock.Text += String.Format("{0}The following type is not CLS-compliant.", nl) + "\n";
      outputBlock.Text += String.Format(str, "SByte  ", xSbyte1, Test(Math.Sign(xSbyte1))) + "\n";
   }
   //
   public static String Test(int compare)
   {
      if (compare == 0)
         return "equal to";
      else if (compare < 0)
         return "less than";
      else
         return "greater than";
   }
}
/*
This example produces the following results:

Test the sign of the following types of values:
Byte   :   0 is equal to zero.
Int16  :  -2 is less than zero.
Int32  :  -3 is less than zero.
Int64  :  -4 is less than zero.
Single :   0 is equal to zero.
Double :   6 is greater than zero.
Decimal:  -7 is less than zero.

The following type is not CLS-compliant.
SByte  : -101 is less than zero.
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.