Version Tolerant Binary Serialization With Microsoft Bond In C#

You’ve been there too. A month ago you wrapped up your first release. You were happy and went for a beer with your workmates. The next month things didn’t go so well. As you start integration testing again, you realize something has gone wrong. You dig down, and discover your app had serialized some useful data to disk not so long ago. Yet, as the new version does its thing, it crashes with a vengeance, when it tries to get that data back from the same place. As you sigh, and realize there won’t be any beer that night, you notice that those serialized bits of data have changed schema between deployments. You think – I do want the app to re-use that data without breaking… So how on Earth do I get around this?

Continue reading Version Tolerant Binary Serialization With Microsoft Bond In C#

How To Distribute Items Into Buckets In C#

The Partition Problem is a very old conundrum in Computer Science. The usual question goes: given some buckets of some capacity, and some items of some size, how can I distribute the items by the buckets in an optimal manner?

The answer to this is more complex than it seems at first glance, as it depends on what you mean by some buckets and optimal.

Continue reading How To Distribute Items Into Buckets In C#

What Is A NullReferenceException And How Do I Fix It?

TLDR;

You get a NullReferenceException when your code is trying to use a null variable. You’re either calling a method on that variable or accessing a property or something else.

The problem is that there’s nothing to call upon to begin with. Either you have never assigned anything to that variable or you have set it to null yourself before trying to use it. Or some external code is returning a null to you, which you are not validating before you using it.

There are many reasons for this exception to pop up…

Continue reading What Is A NullReferenceException And How Do I Fix It?

C#: Why Are My Random Numbers Not Random?

Question

So I have this code, which is supposed to generate a new random number every time it is called:

        public static int GetSomeRandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

In my program, I’m using the above method to let me select five pumpkin colors at random (just don’t ask why):

        enum PumpkinColor
        {
            Red = 0,
            Green = 1,
            Blue = 2
        }

        static void Main(string[] args)
        {
            // write five random pumpkin colors
            for (int i = 0; i < 5; ++i)
            {
                PumpkinColor color = (PumpkinColor)GetSomeRandomNumber(0, 3);
                Console.WriteLine(color);
            }
        }

However, when I run my program, this is the output:

Blue
Blue
Blue
Blue
Blue

Blimey. Not very random now, is it? What gives?

Short Answer

Don’t use a local Random instance in quick loops. Use a shared instance instead:

        private static Random random = new Random();

        public static int GetSomeRandomNumber(int min, int max)
        {
            return random.Next(min, max);
        }

This new implementation now returns:

Green
Red
Red
Blue
Blue

But why does this happen?
And what care do I need to have with static implementations?

Continue reading C#: Why Are My Random Numbers Not Random?

C#: How Can We Enumerate An Enum?

Question

How can I enumerate an enum data type in C#?

Say I have this:

    enum PumpkinVariety
    {
        BabyBear,
        FunnyFace,
        HarvestMoon,
        GhostRider,
        BigMax
    }

How can I list this dynamically in any useful way?

Short Answer

If you have a plain vanilla enum, and you don’t have any funky stuff there such as duplicated values, it’s as easy as this:

foreach (PumpkinVariety pv in Enum.GetValues(typeof(PumpkinVariety)))
{
    // do something here
}

If you do have funky stuff going on, that’s another matter…

Continue reading C#: How Can We Enumerate An Enum?

C#: string or String?

Question

What’s the deal with the string and other types in C#? I see both string and String all over the place. Same goes with int and Int32 and other types.

For example, I see both:

string text = "Some Text Here";

String text = "Some More Text Here";

What is the difference between them?

Short Answer

None. There is no difference between them. Use the one you like best.

Really? No difference whatsoever?
Why have both forms then? What’s the point?

Well…

Continue reading C#: string or String?