Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

decimal in a datatable - == and equals disagreeing

$
0
0

In a simple data table, with a decimal column I am getting different results when using Equals and ==.  I'm sure there is a good reason for this, I just cannot find it - can anyone please explain to me why these would be different?  

using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("amount", typeof(int)));
            dt.Columns.Add(new DataColumn("code", typeof(string)));

            DataRow dr = dt.NewRow();
            dr["amount"] = 10;
            dr["code"] = "A";

            dt.Rows.Add(dr);
            dr.AcceptChanges();

            bool a = dr["amount", DataRowVersion.Current].Equals(dr["amount", DataRowVersion.Original]);
            bool b = dr["amount", DataRowVersion.Current] == dr["amount", DataRowVersion.Original];

            bool c = dr["code", DataRowVersion.Current].Equals(dr["code", DataRowVersion.Original]);
            bool d = dr["code", DataRowVersion.Current] == dr["code", DataRowVersion.Original];
        }
    }
}

While I fully expect c and d to be equal, notice that a and b are not considered equal when using the == operator.  ??


Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>