Which type should I use in C# to represent numbers?

Yesterday I found an old email in my mail box that I thought might be generally interesting.

I was asking the technical lead on the C# compiler which algorithm/shortcut people should use to choose their ‘number types’ among the many available in the language. I was asking for something that works the majority of times, even if not always. I’m sure there are other scenarios we haven’t consider. Anyhow, here is his algorithm.

If you need fractions:

  • Use decimal when intermediate results need to be rounded to fixed precision – this is almost always limited to calculations involving money.
  • Otherwise use double – you will get the rounding of your calculations wrong, but the extra precision of double will ensure that your results will be good enough.
  • Only use float if you know you have a space issue, and you know the precision implications. If you don’t have a PhD in numeric computation you don’t qualify.


  • Use int whenever your values can fit in an int, even for values which can never be negative. This is so that subtraction operations don’t get you confused.
  • Use long when your values can’t fit in an int.

Byte, sbyte, short, ushort, uint, and ulong should only ever be used for interop with C code. Otherwise they’re not worth the hassle.

A good Emerging Market fund

If you are looking for a good fund for this corner of your portfolio, you might want to consider Eaton Vance Structured Emerging Mkt (EAEMX). There is also a much cheaper version (EITEX), but it is open just to folks with a financial advisor.

It is a mechanical fund. Here is how their strategy works (as far as I understand it):

  1. They divide EM market countries in 4 categories, they assign a weight to each category (I.E. 20% to tier1, 30% to tier2 etc…)

  2. For each category they equal weight each country in the category (I.E. in tier1 equal weight Russia, Mexico, etc…)

  3. For each country they equal weight each sector

  4. They actively harvest tax losses

The end result is as follows:

  1. Smaller country have more weight than in a normal Em.Mkt. fund. This is good because:

    1. Smaller markets have had higher returns in the past

    2. Smaller markets are less correlated than large markets both among themselves and against the SP500

  2. Equal weighting the sectors has the following positive effects:

    1. You get more exposure to sectors, like Services, that are going to benefit from economic growth

    2. You avoid the ‘commodity effect’: the tendency of Em.Markets to fall when commodities prices fall because such a large slice of their economy is based on it

  3. It is extremely tax efficient because:

    1. The mechanical strategy lends itself to low turnover

    2. They actively harvest tax losses

In essence they maximize the positive effects of diversification and tax efficiency, which are very stable effects. They are mathematically provable :-) The real time results confirm the theory as this has been one of the best performing Em. Markets funds with the lowest standard deviation and best tax efficiency. No-one knows what the future holds, but there is a good chance that this will continue to be the case.

I don’t own it currently because I don’t like the high fees, but the strategy makes so much sense that I might end up buying into it.

Apparently simple code …

Sometimes what looks simple is complex and what looks complex is simple. See if you can understand how this one calculates all the possible ways to give change for a certain amount of money given some kinds of coins. You MIT guys out there don’t count, you probably have read the solution in the same book I have.

BTW: the code works with the LINQ May CTP …

using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;

class Program
static void Main(string[] args)
var coins = new int[] { 1, 5, 10, 25, 50 };

var i = ChangeComb(100, coins);

static int ChangeComb(int amount, IEnumerable<int> coins)
if (amount == 0) return 1;
if (amount < 0) return 0;
if (coins.Count() == 0) return 0;

return ChangeComb(amount, coins.Skip(1)) +
ChangeComb(amount – coins.First(), coins);

.csharpcode, .csharpcode pre
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
background-color: #f4f4f4;
width: 100%;
margin: 0em;
.csharpcode .lnum { color: #606060; }