You cannot assume that a string with the same value points to the same memory location. So the mmemory can contain two or more instances with 'ABC'. In that case your lock will not work.
Luckily .NET has a trick: string.Intern()
. See: http://msdn.microsoft.com/en-us/library/system.string.intern(v=vs.110).aspx
This function will store or locate your string in some kind of internal dictionary. If the value is new, he will add it into the dictionary. If it already exists, the original value is returned, resulting in one address to strings with the same value.
public string AppendOne(string ID)
{
lock (string.Intern(ID))
{
Thread.Sleep(5000);
return ID+"One";
}
}
Warning: If ID is null, you will get a exception. You might wanna test on that first.