I am keeping an global object in application cache, retrieving and modifying it based on my needs. The weird thing is once I modify the retrieved object the application object is getting modified too. I don’t want the application object to be modified.
System.Web.HttpContext.Current.Application["obj2"]=obj2;
like
object obj1 = System.Web.HttpContext.Current.Application["obj2"];
when I modify obj1 the obj2 is getting modified too. Can you guys suggest me the solution for the above problem?
Objects in C# are always by-reference, which means that whenever you access this object it will always be the same instance to the object. It sounds like what you might want to do is to clone it first then modify the clone instead. This is also important for shared memory multi-threaded environments like what you have above.
Also, you might consider making the object that you are caching a struct, which is passed by-value instead of by-reference, as long as you unbox it first. In general you don’t want to edit objects shared by threads.