From the course: Visual Studio Developer Tips

Unlock this course with a free trial

Join today to access over 22,400 courses taught by industry experts.

Use the nameof operator

Use the nameof operator

- [Instructor] Strings are an indispensable and necessary part of any programming language, but like any prominent programming feature, they can be misused. Using a string as an output value is reasonable. Storing strings in databases is okay. Using strings to refer to properties, methods, classes, or other code constructs, so-called magic strings, is less desirable. Why are magic strings considered undesirable by the developer community? I listed a few of the reasons here in the comments. One, it's easy to mistype a string literal. When you use string literals, they tend to be duplicated or repeated everywhere. Three, it's always easier to refactor strongly typed identifiers. And finally, when you work with tools like Visual Studio IntelliSense and Auto Complete, magic strings are invisible to those tools. This tip looks at the C Sharp nameof operator, which addresses some of these concerns. The nameof operator is used to obtain the string name of a variable, type or member. Here's…

Contents