AvaloniaUI / Avalonia.Markup.Declarative

Provides helpers for declarative ui in C#
372 stars 21 forks source link

Use nameof for caller argument expression #69

Closed WeihanLi closed 1 week ago

WeihanLi commented 3 weeks ago

use nameof for CallerArgumentExpression

gritsenko commented 2 weeks ago

@WeihanLi hey! thanks for the PR! But can you explain why exactly did you decide to make those changes?

WeihanLi commented 2 weeks ago

@gritsenko using the nameof expression, the compiler would help us check whether the param name is valid and it would be helpful when doing some refactoring especially when renaming

For example, when we change the param name from val to val1, the nameof would report an error while the hard-coded param name would not, though it references a param not exist

image

And it's also used in the dotnet runtime code, for example:

image

source code link: https://github.com/dotnet/runtime/blob/e4fe27d8be89ac6805d9f2d4e92295a3322364a9/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs#L55

Hope it explains, thanks