次の方法で共有


Decimal.Equals メソッド (Object)

このインスタンスと指定した Object が同じ型と値を表しているかどうかを示す値を返します。

Overrides Overloads Public Function Equals( _
   ByVal value As Object _) As Boolean
[C#]
public override bool Equals(objectvalue);
[C++]
public: bool Equals(Object* value);
[JScript]
public override function Equals(
   value : Object) : Boolean;

パラメータ

戻り値

value が Decimal で、このインスタンスと等しい場合は true 。それ以外の場合は false

使用例

[Visual Basic, C#, C++] Equals メソッドを使用して、 Decimal の複数の値および他のオブジェクトを Decimal の参照値と比較するコード例を次に示します。

 
' Example of the Decimal.CompareTo and Decimal.Equals instance methods.
Imports System
Imports Microsoft.VisualBasic

Module DecCompToEqualsObjDemo
    
    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( ex As Exception ) As String

        Dim exceptionType   As String = ex.GetType( ).ToString( )
        Return exceptionType.Substring( _
            exceptionType.LastIndexOf( "."c ) + 1 )
    End Function

    ' Compare the Decimal to the Object parameters, 
    ' and display the Object parameters with the results.
    Sub CompDecimalToObject( Left as Decimal, Right as Object, _
        RightText as String )

        Console.WriteLine( "{0,-46}{1}", "Object: " & RightText, _
            Right )
        Console.WriteLine( "{0,-46}{1}", "Left.Equals( Object )", _
            Left.Equals( Right ) )
        Console.Write( "{0,-46}", "Left.CompareTo( Object )" )

        ' Catch the exception if CompareTo( ) throws one.
        Try
            Console.WriteLine( "{0}" & vbCrLf, _
                Left.CompareTo( Right ) )
        Catch ex As Exception
            Console.WriteLine( "{0}" & vbCrLf, _
                GetExceptionType( ex ) )
        End Try
    End Sub

    Sub Main( )
        Console.WriteLine( _
            "This example of the Decimal.Equals( Object ) " & _
            "and " & vbCrLf & "Decimal.CompareTo( Object ) " & _
            "methods generates the " & vbCrLf & _
            "following output. It creates several different " & _
            "Decimal " & vbCrLf & "values and compares them " & _
            "with the following reference value." & vbCrLf )

        ' Create a reference Decimal value.
        Dim Left as New Decimal( 987.654 )

        Console.WriteLine( "{0,-46}{1}" & vbCrLf, _
            "Left: Decimal( 987.654 )", Left )

        ' Create objects to compare with the reference.
        CompDecimalToObject( Left, New Decimal( 9.8765400E+2 ), _
            "Decimal( 9.8765400E+2 )" )
        CompDecimalToObject( Left, 987.6541D, "987.6541D" )
        CompDecimalToObject( Left, 987.6539D, "987.6539D" )
        CompDecimalToObject( Left, _
            New Decimal( 987654000, 0, 0, false, 6 ), _
            "Decimal( 987654000, 0, 0, false, 6 )" )
        CompDecimalToObject( Left, 9.8765400E+2, _
            "Double 9.8765400E+2" )
        CompDecimalToObject( Left, "987.654", _
            "String ""987.654""" )
    End Sub
End Module 

' This example of the Decimal.Equals( Object ) and
' Decimal.CompareTo( Object ) methods generates the
' following output. It creates several different Decimal
' values and compares them with the following reference value.
' 
' Left: Decimal( 987.654 )                      987.654
' 
' Object: Decimal( 9.8765400E+2 )               987.654
' Left.Equals( Object )                         True
' Left.CompareTo( Object )                      0
' 
' Object: 987.6541D                             987.6541
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      -1
' 
' Object: 987.6539D                             987.6539
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      1
' 
' Object: Decimal( 987654000, 0, 0, false, 6 )  987.654000
' Left.Equals( Object )                         True
' Left.CompareTo( Object )                      0
' 
' Object: Double 9.8765400E+2                   987.654
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      ArgumentException
' 
' Object: String "987.654"                      987.654
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      ArgumentException

[C#] 
// Example of the decimal.CompareTo and decimal.Equals instance 
// methods.
using System;

class DecCompToEqualsObjDemo
{
    // Get the exception type name; remove the namespace prefix.
    public static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    // Compare the decimal to the object parameters, 
    // and display the object parameters with the results.
    public static void CompDecimalToObject( decimal Left, 
        object Right, string RightText )
    {

        Console.WriteLine( "{0,-46}{1}", "object: "+RightText, 
            Right );
        Console.WriteLine( "{0,-46}{1}", "Left.Equals( object )", 
            Left.Equals( Right ) );
        Console.Write( "{0,-46}", "Left.CompareTo( object )" );

        try
        {
            // Catch the exception if CompareTo( ) throws one.
            Console.WriteLine( "{0}\n", Left.CompareTo( Right ) );
        }
        catch( Exception ex )
        {
            Console.WriteLine( "{0}\n", GetExceptionType( ex ) );
        }
    }

    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the decimal.Equals( object ) and \n" +
            "decimal.CompareTo( object ) methods generates the \n" +
            "following output. It creates several different " +
            "decimal \nvalues and compares them with the following " +
            "reference value.\n" );

        // Create a reference decimal value.
        decimal Left = new decimal( 987.654 );

        Console.WriteLine( "{0,-46}{1}\n", 
            "Left: decimal( 987.654 )", Left );

        // Create objects to compare with the reference.
        CompDecimalToObject( Left, new decimal( 9.8765400E+2 ), 
            "decimal( 9.8765400E+2 )" );
        CompDecimalToObject( Left, 987.6541M, "987.6541D" );
        CompDecimalToObject( Left, 987.6539M, "987.6539D" );
        CompDecimalToObject( Left, 
            new decimal( 987654000, 0, 0, false, 6 ), 
            "decimal( 987654000, 0, 0, false, 6 )" );
        CompDecimalToObject( Left, 9.8765400E+2, 
            "Double 9.8765400E+2" );
        CompDecimalToObject( Left, "987.654", "String \"987.654\"" );
    }
}

/*
This example of the decimal.Equals( object ) and
decimal.CompareTo( object ) methods generates the
following output. It creates several different decimal
values and compares them with the following reference value.

Left: decimal( 987.654 )                      987.654

object: decimal( 9.8765400E+2 )               987.654
Left.Equals( object )                         True
Left.CompareTo( object )                      0

object: 987.6541D                             987.6541
Left.Equals( object )                         False
Left.CompareTo( object )                      -1

object: 987.6539D                             987.6539
Left.Equals( object )                         False
Left.CompareTo( object )                      1

object: decimal( 987654000, 0, 0, false, 6 )  987.654000
Left.Equals( object )                         True
Left.CompareTo( object )                      0

object: Double 9.8765400E+2                   987.654
Left.Equals( object )                         False
Left.CompareTo( object )                      ArgumentException

object: String "987.654"                      987.654
Left.Equals( object )                         False
Left.CompareTo( object )                      ArgumentException
*/ 

[C++] 
// Example of the Decimal::CompareTo and Decimal::Equals instance 
// methods.
#using <mscorlib.dll>
using namespace System;

// Get the exception type name; remove the namespace prefix.
String* GetExceptionType( Exception* ex )
{
    String* exceptionType = ex->GetType( )->ToString( );
    return exceptionType->Substring( 
        exceptionType->LastIndexOf( '.' ) + 1 );
}

// Compare the Decimal to the Object parameters, 
// and display the Object parameters with the results.
void CompDecimalToObject( Decimal Left, 
    Object* Right, String* RightText )
{
    Console::WriteLine( S"{0,-46}{1}", 
        String::Concat( S"Object: ", RightText ), Right );
    Console::WriteLine( S"{0,-46}{1}", S"Left.Equals( Object )", 
        __box( Left.Equals( Right ) ) );
    Console::Write( S"{0,-46}", S"Left.CompareTo( Object )" );

    try
    {
        // Catch the exception if CompareTo( ) throws one.
        Console::WriteLine( S"{0}\n", 
            __box( Left.CompareTo( Right ) ) );
    }
    catch( Exception* ex )
    {
        Console::WriteLine( S"{0}\n", GetExceptionType( ex ) );
    }
}

void main( )
{
    Console::WriteLine( 
        S"This example of the Decimal::Equals( Object* ) and \n" 
        S"Decimal::CompareTo( Object* ) methods generates the \n" 
        S"following output. It creates several different " 
        S"Decimal \nvalues and compares them with the following " 
        S"reference value.\n" );

    // Create a reference Decimal value.
    Decimal Left = Decimal( 987.654 );

    Console::WriteLine( S"{0,-46}{1}\n", 
        S"Left: Decimal( 987.654 )", __box( Left ) );

    // Create objects to compare with the reference.
    CompDecimalToObject( Left, __box( Decimal( 9.8765400E+2 ) ), 
        S"Decimal( 9.8765400E+2 )" );
    CompDecimalToObject( Left, 
        __box( Decimal::Parse( S"987.6541" ) ), 
        S"Decimal::Parse( \"987.6541\" )" );
    CompDecimalToObject( Left, 
        __box( Decimal::Parse( S"987.6539" ) ), 
        S"Decimal::Parse( \"987.6539\" )" );
    CompDecimalToObject( Left, 
        __box( Decimal( 987654000, 0, 0, false, 6 ) ), 
        S"Decimal( 987654000, 0, 0, false, 6 )" );
    CompDecimalToObject( Left, __box( 9.8765400E+2 ), 
        S"Double 9.8765400E+2" );
    CompDecimalToObject( Left, S"987.654", S"String \"987.654\"" );
}

/*
This example of the Decimal::Equals( Object* ) and
Decimal::CompareTo( Object* ) methods generates the
following output. It creates several different Decimal
values and compares them with the following reference value.

Left: Decimal( 987.654 )                      987.654

Object: Decimal( 9.8765400E+2 )               987.654
Left.Equals( Object )                         True
Left.CompareTo( Object )                      0

Object: Decimal::Parse( "987.6541" )          987.6541
Left.Equals( Object )                         False
Left.CompareTo( Object )                      -1

Object: Decimal::Parse( "987.6539" )          987.6539
Left.Equals( Object )                         False
Left.CompareTo( Object )                      1

Object: Decimal( 987654000, 0, 0, false, 6 )  987.654000
Left.Equals( Object )                         True
Left.CompareTo( Object )                      0

Object: Double 9.8765400E+2                   987.654
Left.Equals( Object )                         False
Left.CompareTo( Object )                      ArgumentException

Object: String "987.654"                      987.654
Left.Equals( Object )                         False
Left.CompareTo( Object )                      ArgumentException
*/ 

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Decimal 構造体 | Decimal メンバ | System 名前空間 | Decimal.Equals オーバーロードの一覧 | Compare | CompareTo