Category Archives: C#

Six months of Windows Phone Development — Tips, tricks, and performance considerations

A stroke order diagram for the character 羲 TL;DR: I’ve taken plenty of notes while developing my YiXue Chinese dictionary for Windows Phone. Topics covered in this article include performance tips, best practices, and plenty of code snippets for Windows Phone.

Introduction

This article presents notes and remarks that I gathered while working on a Chinese Dictionary App for Windows Phone, YiXue Chinese Dictionary: mistakes I made, fun tips I wrote down, and so on.
I initially didn’t really intend to create a full blog post out of these notes, but their increasing number, and my app recently placing second in Microsoft France’s App Awards contest, gave me enough motivation to share them with the community. Along with various tips and tricks and answers to often-asked (but seldom answered) questions, I will discuss a number of performance improvements that specifically apply to Windows Phone apps.

Continue reading

Modeling and measuring string comparison performance in C, C++, C# and Python.

O(1) Comparing strings is often — erroneously — said to be a costly process. In this article I derive the theoretical asymptotic cost of comparing random strings of arbitrary length, and measure it in C, C++, C# and Python.

Continue reading

Using abstract classes to simulate tagged unions (aka sum types)

Most functional languages offer support for tagged unions (also called sum types), a type of data structure capable of successively holding values of several fixed types. This article shows how to use abstract classes to emulate such behaviour in high-level object-oriented languages such as C#, Java, or VB.Net ((.Net languages have the [StructLayout(LayoutKind.Explicit)] attribute, which makes it possible to create structs which behave a lot like C++ unions. But that only works with primitive types.)).

Continue reading

A safer way to implement boolean flags

Introduction

Boolean flags are commonly used to disable a block of code while another is running: for example,

  private bool in_use;
  
  private void Process() {
    in_use = true;
    (...)
    in_use = false;
  }
  
  private void OnEvent() {
    if (in_use)
      return;
      
    (...)
  }
  Private InUse As Boolean;
  
  Private Sub Process()
    InUse = True
    (...)
    InUse = False
  End Sub
  
  Private Sub Process()
    If InUse Then Return
    
    (...)
  End Sub

This design has a major drawback : in_use blocks cannot be nested, since nesting two such blocks will turn in_use to false too early. In the following excerpt, the (...) section in the Process function will not run correctly, because in_use will have been set to false when it runs.

  private void Process() {
    in_use = true;
    Action1();
    Action2();
    (...) // in_use == false here (!)
    in_use = false;
  }

  private void Action1() {
    in_use = true;
    (...)
    in_use = false;
  }

  private void Action2() {
    in_use = true;
    (...)
    in_use = false;
  }
  Private Sub Process()
    InUse = True
    Action1()
    Action2()
    (...) ' InUse = False here (!)
    InUse = False
  End Sub

  Private Sub Action1()
    InUse = True
    (...)
    InUse = False
  End Sub

  Private Sub Action2()
    InUse = True
    (...)
    InUse = False
  End Sub

Such errors are difficult to spot in large applications, and often lead to hard-to-track bugs.

Continue reading