Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove string.Format() if nameof() is used #17356

Closed
dbeuchler opened this issue Feb 24, 2017 · 2 comments
Closed

Remove string.Format() if nameof() is used #17356

dbeuchler opened this issue Feb 24, 2017 · 2 comments

Comments

@dbeuchler
Copy link

dbeuchler commented Feb 24, 2017

For some strings it is usefull to use the nameof()-Operator but the resulting string.Format() is not really needed.

Exsample
Exceptions can be defined like this:
throw new InvalidCastException($"The type cannot be casted to {nameof(IMyInterface)}");

Version Used: Visual Studio 2015 Update 3, .NET Framework 4.5.2

Expected Behavior:
The compiler should remove the string.Format()-Method and use the string itself.
throw new InvalidCastException("The type cannot be casted to IMyInterface");

Actual Behavior:
The result of the compiler is:
throw new InvalidCastException(string.Format("The type cannot be casted to {0}", "IMyInterface"));

IL_001f: nop
IL_0020: ldstr "The type cannot be casted to {0}"
IL_0025: ldstr "IMyInterface"
IL_002a: call string [mscorlib]System.String::Format(string, object)
IL_002f: newobj instance void [mscorlib]System.InvalidCastException::.ctor(string)
IL_0034: throw
@svick
Copy link
Contributor

svick commented Feb 24, 2017

This is a duplicate of #11259.

@IanKemp
Copy link
Contributor

IanKemp commented Dec 13, 2018

Not a duplicate - this deals with only the very specific case of nameof.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants