Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have seen in so many places people uses
C#
public const string Home = "Test";

Instead why don't they use value "Test" at the places they need?
what is the advantage of using const against hard coded string

What I have tried:

I have seen in so many places people uses
C#
public const string Home = "Test";

Instead why don't they use value "Test" at the places they need?
what is the advantage of using const against hard coded string
Posted
Updated 19-Mar-16 21:44pm

Imagine that this string value has to change at some point. If it's hardcoded everywhere in the application, it has to be replaced everywhere, which is a waste of time. With a constant as in your example, you just have to replace it at one place and then it's replaced everywhere.
 
Share this answer
 
Comments
MYQueries1 20-Mar-16 2:59am    
I got that. Is there any difference of using constant string instead of HardCoded string when i know that i am going to use that only in one place.
Tomas Takac 20-Mar-16 3:40am    
No. The compiler treats constants and literals the same way. Read about string interning.
Afzaal Ahmad Zeeshan 20-Mar-16 3:46am    
If you are going to use it in one place, then chances are you should use it as a hardcoded string; instead of creating a new memory allocation separately. Using a variable is preferred if you are going to use an item multiple times, and that is why you need to focus on DRY rule too.

Read my answer, Solution 3, for more.
First thing to keep in mind is that all software evolve as time passes by. If not you then somebody else has to modify/maintain the code you wrote. So to make life easy and as a standard practice all strings which has some predefined value and which is not going to change during execution of program should me made constant.

Advantage? If you or somebody else has to change it , it will be at a single place. Means less error prone maintenance. And why put const in-front of it, to make sure your intention clear to other developers too that this string is not going to change for the execution of program.
 
Share this answer
 
Hardcoded string are the literal strings. So, What you may be referring to is, literal strings in the data.
C#
// Instead of
var myStr = Home + "123";

// Using this
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:
C#
// C# 6 string interpolation
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900