Strongly typed constant parameters in C#

After a bit of searching, I found a way to have strongly typed constant parameters for C# functions. You know the situation, where you need to pass one of a limited set of strings or chars or other values to a function and you want to make sure somebody doesn’t just go and pass any old thing they find laying around the place. Enums are pretty good for this kind of thing, but it gets hairy if you need to translate it to anything else, like a string or a char.

Any solution also needs to pander to intellisense, making it easy to use and kinda idiot safe (I’m talking about myself a couple of hours after defining any constant, which usually leads to me behaving like the idiot user I had a hard time envisioning just hours earlier).

I think I found a good system for doing this, and as an example, I’ll invent a function that takes a string parameter, but it has to be just the right kind of string. To do that, I first declare the constant strings in a separate module this way:

Then I write my function, the fictional “Rechandler” that takes a parameter of the ConstRecTypeValue kind. And then I write a function that calls it. Now, while writing the caller, I want intellisense to do its thing, and it does:

As you can see, it obediently pops up a tooltip to tell me only a ConstRecTypeValue is accepted here. As soon as I start to type that, it recognizes the ConstRecType static class name and it intellisensively lets me choose which constant member I want:

…which I complete the usual way:

The callee (Rechandler) then easily recovers the string that is hiding inside the passed value (in this case “DELETED”) and continues its merry ways.

Naturally, you can use chars, doubles or entire collections of values instead of the string value in this example and still achieve the same effect.

You can also take it one step further along the path to universality, by using a generic base class for the value type:

If you have this guy in reach in your project somewhere, you can now simplify the definition of the value class like so:

…while everything else stays just the same.

I love it.

3 thoughts on “Strongly typed constant parameters in C#”

  1. Reminiscent of what Cocoa’s always been doing, e g notification names, like

    NSString *const UIWindowDidBecomeVisibleNotification.

    Typesafe, and expandable without reserving enum numbers (e g if a third party subclasses UIWindow and adds a notification, which mustn’t conflict with any other third party’s UIWindow subclass notification), but to get autocompletion you have to know it starts with UIWindow and then autocomplete from there, so not as convenient perhaps. (Especially since “Notification” is at the end so you have to autocomplete for UIWindow* and not UIWindowNotification*)

  2. … and yes, that’s almost exactly what you do in the blog post before this. Never mind me! Like your blog btw, keeping me very unproductive 😉

  3. Thanks for the kind words, and I’m glad I can help keep you away from work… 🙂

Comments are closed.