.NET Tips and Tricks

Blog archive

Guru Tips: Making ToString Useful, Plus Not Getting Fooled by Closure

Julian Bucknall is the Chief Technology Officer at DevExpress. I asked him if he had a tip for .NET developers. Julian said he had two (and that doesn't count the bonus he threw in).

First Tip: ToString() is your friend. When the CLR team wrote System.Object, they included the very minimum of methods needed for all objects. All of them are 100 percent vital, no fat here. One of them is ToString(), the method that provides a human-readable string of the important properties in your class -- especially important for debugging. The default is boring and uninformative: you should work on creating your own overrides for your own classes. Your fellow developers will thank you, since they won't have to go spelunking through your class' properties when debugging -- they can just call your ToString method.

Bonus Asp.Net Tip: Do the same with the toString method for your prototypes in JavaScript. Much more informative than getting back the default "[object Object]'.

Second tip: Closures capture variables, not values. Writing anonymous functions is great fun, but beware: If an anonymous function captures a local variable, it captures the variable, not the current value of that variable at the time the anonymous function is defined.

This code has two anonymous functions, both of which use the variable called constantValue. The code sets constantValue before defining each function, but, when the functions are executed it's the latest value of constantValue that's used:

delegate int AddConst(int x);
  
  class Program {
    static void Main(string[] args) {


      var constantValue = 7;
      AddConst AddSeven = (x) => { return x + constantValue; };


      constantValue = 2;
      AddConst AddTwo = (x) => { return x + constantValue; };


      Console.WriteLine(AddSeven(4)); // result: 6 !?!
      Console.WriteLine(AddTwo(4));   // result: 6

      Console.ReadLine();
    }
  }

Here, the AddSeven and AddTwo functions aren't capturing the current value of constantValue when the functions are created; they're capturing the variable. When the functions are run, they use whatever the value of currentValue is at the time of execution. Beware!

Posted by Peter Vogel on 02/16/2012


comments powered by Disqus

Featured

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube