I'm experimenting with [CallerArgumentExpression] in C# 8:
static void Main(string[] args)
{
try
{
Program query = null;
Argument(query != null, "Ooops");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public static void Argument(bool condition, string message, [CallerArgumentExpression("condition")] string conditionExpression = null)
{
if (!condition) throw new ArgumentException(message: message, paramName: conditionExpression);
}
However, I cannot get the value of conditionExpression
to be anything other than null.
I've been using this https://blog.mcilreavy.com/articles/2018-08/caller-argument-expression-attribute and a few other pages and, of course, good as a guide, but I can't get it to work.
This page https://www.c-sharpcorner.com/article/c-sharp-8-features/ claims that it never made it to C#8 and is now C# Next.
See also https://github.com/dotnet/csharplang/issues/287 especially towards the bottom.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With