Can we get an overload of TryGetValue() without the side effect?

Topics: APIs
Aug 2, 2014 at 5:06 AM
If I understand correctly, C# 6's "declaration expressions" would allow the following:
var foo = "foo";
if( (var foo2 = foo.ToUpper()) == "FOO") {
   Response.Write(foo2);  // outputs FOO
If this is the case, could we get an pure function overload of TryGetValue() that works something like this:
public struct TryGetValueResult<TValue> {
   public readonly bool Success;
   public readonly TValue Value;
   static public implicit operator bool(TryGetValueResult<TValue> result) {
       return result.Success;

public class Dictionary<TKey, TValue> {
   public TryGetValueResult<TValue> TryGetValue(TKey key);

if(var result = dict.TryGetValue("mykey") {
I could always write an extension of course, but having this in the BCL would provide an alternative for developers who prefer to avoid "out" parameters.

The implicit conversion to boolean was just added for more syntactic sugar, one could always use if((var...).Success) instead. An implicit conversion to TValue might also be nice.