activa's blog

.NET, Web, Mobile and more stuff I can't stop talking about

Browsing Posts tagged C#

The iPhone is a fabulous device, few people will argue about that. But… When I started developing iPhone apps about a year ago, it felt… awkward. If you want to create an app for the iPhone, you have to use Objective C, which was invented sometime around 1934. I’m kidding of course: it was 1986, [...]

We all love unit tests (do we?). Yes we do (repeat 100 times). And we all love mocking for making life easier, but sometimes we just want to write some simple unit tests that don’t require any mocking at all, because the code you are testing is easy to test without having to resort to [...]

First some basics: in .NET there are reference types and value types. Reference types are always accessed through a reference (a pointer actually) while value types, well, are not. The .NET documentation tries to sum it up in one sentence: A data type is a value type if it holds the data within its own [...]

This is another one in the series “heck, I never thought of that”… Like most of these articles, if you already knew this trick, ignore me… Let’s say you have a generic class with a new() constraint on the type parameter. This means that you are allowed to create new objects of the generic type, [...]

Like many tips & tricks concerning programming languages, what I will present here will be so utterly obvious to some C# developers, but could be an eye-opener to others. How often do you write something like this? if (token == "A") tokenNumber = 1; else if (token == "B") tokenNumber = 4; else if (token [...]

How many times have you written some timing code around a method call? If the answer is never, move on, skip this article… For timing purposes, I always use a simple “TimeRunner” class that allows you to accurately measure the time it takes to execute a specific method. A kind of “poor man’s profiler”. The [...]

Constructing related .NET objects (an “object graph”) from XML files is a familiar challenge to most developers out there. Although it is not hard to write something like that: just read the tokens and fill the appropriate objects. In most cases, the code produced is not very readable and hard to maintain, because the problem [...]

Working with nullable types using reflection is not that hard, but sometimes you have to think about it for a bit or browse the help file before you can actually use it correctly. What I will list here is not rocket science, but it could be helpful to have all the nullable-related stuff in one [...]

This article may be extremely obvious to many people, but I am sure a lot of C# developers will be surprised by what will follow. What’s the difference between MyType myObject1 = (MyType) anotherObject; and: MyType myObject2 = anotherObject as MyType; Well, when “anotherObject” is assignable to MyType, both lines of code do the exact [...]

We all know the Convert.ChangeType() for changing a value from one (unknown) type to another (unknown) type at runtime, but this method doesn’t handle nullables and enums very well. What may seem a simple task isn’t that simple after all. You have to differentiate nullables, value types and enums. I threw in a little recursion, [...]