I have some code that uses the GetOrAdd function from ConcurrentDictionary to add values with a unique key to a holder. However, I want to be able to tell if a values key is found (i.e. a true returned from TryGet) or whether it has to be added.
I haven't been able to find anything online that suggests this is achievable so if anyone has any ideas it would be really helpful.
Not consistently, no. If the method returns something other than the value that you provide then you know that the key already existed but if the key exists and has the same value that you provide then there will be no distinction. If you want to know definitively then you will have to test for the key first. It sounds like your best bet is to call
ContainsKeyfirst and then callGetOrAdd. You could wrap that into your own single method so it's easy to reuse and lock the dictionary if you need it to be atomic.