I hate using strings to represent program elements! One of the big problems is that it hinders automated refactoring. Strings don’t have any semantic meaning, so refactoring tools don’t know which references to the string need to be updated.
While playing with expression trees, a while ago I came up with this:
public static class Argument
public static void NotNull(object value, Expression<Func<Func<object>>> arg)
if (value == null)
var body1 = (Expression<Func<object>>)arg.Body;
var body2 = (MemberExpression)body1.Body;
throw new ArgumentNullException(body2.Member.Name);
You can use this for argument validation like this:
static void Main(string args)
Argument.NotNull(args, () => () => args);
This looks a little strange, but has some interesting properties:
On the other hand it has some drawbacks too:
I’m not sure I would use this in production code, but it was an interesting exercise to come up with a way of validating parameters that doesn’t involve strings.
What do you think?