InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Making 0 Equal 0 in C#

Posted by Jonathan Allen on Jun 01, 2009

Sections
Development,
Architecture & Design
Topics
Language Design ,
.NET
Tags
C# 4

C# does not work well with boxed numerical values. Unlike Visual Basic, the basic numeric comparison operators such as == do not work with boxed types even when both values are the same type.

Variable Type Value
a int 0
b decimal 0.0
c decimal 0.0
boxA boxed int 0
boxB boxed decimal 0.0
boxC boxed decimal 0.0
dynA dynamic holding an int 0
dynB dynamic holding a decimal 0.0
dynC dynamic holding a decimal 0.0

Comparison C# VB
a==b true true
b==a true true
b==c true true
a.Equals(b) false <--
b.Equals(c) true <--
boxA == boxB false true
boxB == boxA false true
boxB == boxC false true
boxA.Equals(boxB) false <--
boxB.Equals(boxC) true <--
dynA == dynB true n/a
dynB == dynA true n/a
dynB == dynC true n/a

As you can see, using C# 3 and earlier even two boxed decimals with the same value will evaluate as being unequal. This occurs even when the Equals method on the Decimal class would otherwise return true.

Fortunately with C# 4 you can avoid these problems. By first casting the boxed values as dynamic, you do get the correct results even when comparing different types.

Console.WriteLine((dynamic)boxA == (dynamic)boxB);
Re: Making 0 Equal 0 in C# by Stefan Wenig Posted
Re: Making 0 Equal 0 in C# by Francois Ward Posted
Re: Making 0 Equal 0 in C# by Stefan Wenig Posted
  1. Back to top

    Re: Making 0 Equal 0 in C#

    by Stefan Wenig

    I like the phrase "does not work well". C# has well-defined rules that avoid guesswork. Not just for the compiler, but also for the human reader. Trying to be too smart would make other (more likely cases) harder to read. VB is better in this respect only if you do trial-and-error coding.

    To compare boxed values of the same type, use the static method Object.Equals (a, b). This is much more explicit than using "dynamic".

    Comparing boxed integers to boxed decimals smells bad in any case. If you really need it, being a bit more explicit about it won't hurt.

  2. Back to top

    Re: Making 0 Equal 0 in C#

    by Francois Ward

    I agree. This was all designed purposely. It was the design we wanted. C# was a very very "static" language, where (most) everything had to be explicit. If for a particular project, I didn't want these behaviors, I would use something else (like VB!).

    Now I feel we're losing an option instead of gaining some.

  3. Back to top

    Re: Making 0 Equal 0 in C#

    by Stefan Wenig

    What are we losing? "dynamic" is opt-in, and it consistently behaves as you would expect a dynamic language to. Don't want it, use "object".

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.