public class Sample{
private const ="constant string";
public static object StaticMethod(args){
var result= SomeOtherClass.StaticMethod(const,args);
return result;
}
}
will this code be thread safe?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Constants are thread safe by every means, As they are compiled into to their real values during compile time and their values never change during execution of program regardless of how many threads are accessing it 🙂
Once you will compile the code compiler will replace all references of const variable with constant’s real value in your case it is “Constant String”
hence it will never change in its life time. until you change the value of constant variable it self and recompile the code.
I gave reference from Eric Lippert as he is a principal developer on the C# compiler team.
From Eric’s blog