Are Object Equal - CSharp System

CSharp examples for System:Object

Description

Are Object Equal

Demo Code

// This program is free software; you can redistribute it and/or modify
using System.Text;
using System.Collections.Generic;
using System;//from w w  w  .  j ava 2s  .  c  om

public class Main{
        public static void AreEqual(object expected, object value)
        {
            AreEqual(expected, value, null);
        }
        public static void AreEqual(object expected, object value, string msg)
        {
            try
            {
                if (expected is UInt64)
                {
                    ulong lValue = Convert.ToUInt64(value);
                    if (!expected.Equals(lValue))
                        throw new Exception(msg);
                }
                else
                {
                    long iExpected = Convert.ToInt64(expected);
                    long iValue = Convert.ToInt64(value);
                    if (iExpected != iValue)
                        throw new Exception(msg);
                }
            }
            catch (Exception ex)
            {
                if (ex is InvalidCastException ||
                    ex is FormatException)
                {
                    if (!expected.Equals(value))
                        throw new Exception(msg);
                }
                else
                    throw new Exception(msg);
            }

        }
}

Related Tutorials