.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

  • See Prompts Microsoft Engineers Use for Bleeding-Edge Multimodal RAG AI Research

    Vision-centric queries show how front-line experts are prompting LLMs these days.

  • AI Explains Expressions in Update to Java on VS Code

    "The Spring Tools now show code lenses above these expressions that allow you to quickly let GitHub Copilot explain those statements for you."

  • Microsoft Eases Integration with Semantic Kernel AI SDK

    The basic idea is to provide unified API abstractions, especially for idiomatic C# code, to help platform developers and others work with any provider with standard implementations for caching, telemetry, tool calling and other common tasks.

  • Final .NET 9 Preview Ships with Go-Live License

    Visual Studio developers can now download the SDK for .NET 9 Release Candidate 2 with a go-live license, meaning devs get Microsoft support for production applications even before the framework reaches general availability next month.

Subscribe on YouTube