Hardcoded string are the literal strings. So, What you may be referring to is, literal strings in the data.
var myStr = Home + "123";
var myStr = "Test123";
The problem doesn't show up, unless we have to use this string literal in multiple places. If you are going to use this "Test" in one location, then of course, you can use it there. The difference is when you need to use its value in many places.
Now, encounter and assume this scenario. Suppose, you are an ASP.NET developer, and you are building URLs using the current active Controller. Suppose, you are having a
Home
controller for deployment, but for development environment you are using
Test
controller. You can use the following code:
var url = $"http://www.example.com/{Home}/{Action}";
If this is what you are using, then chances are that you won't have to make sure that you have updated the URLs at every instance. Instead, you can just alter the value of this Home field to "
Home
" and you're good to go.
Do not forget: A string object is
anyway constant. That is, you need to create a new object for new strings. Immutability rules here. From this document at MSDN:
string (C# Reference)[
^], you can get to know that either way a string is constant. Using
const
is just to say,
we are also not going to modify the field.