I was just reading The Pragmatic Programmer and the Design By Contract chapter. It occurred to me that when we make a function maybe in a common library, specifying attributes of parameters would be a useful thing.
I’m wondering if anyone has come across such an idea, I don’t believe it exists in C# quite how I’m thinking which is something like this;
public String SendEmail(String ToAddress [RegExMatch(@"\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b")], FromAddress, Body)
For readability it would make more sense to put it above the function declaration I think.
I can’t imagine it would be a bad idea as the function could automatically throw an exception for invalid parameters. But it would just give a bit more definition than String or Int32.
More of a discussion than looking for an answer.
Cheers,
Mike
Perhaps it would be better to just create a new type that deals with that kind of stuff. You could have an EmailAddress type which validates any string arguments it gets when constructed or modified. Then there is no need for attribute trickery.