Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CallerArgumentExpression always null

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.

like image 585
Paul Grenyer Avatar asked Nov 18 '20 16:11

Paul Grenyer


1 Answers

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.

like image 169
RichardHowells Avatar answered Sep 22 '22 19:09

RichardHowells