// bool TryGetValue(TKey key, out TValue value)
        internal bool TryGetValue <K, V>(K key, out V value)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            IMapView <K, V> _this = JitHelpers.UnsafeCast <IMapView <K, V> >(this);

            // It may be faster to call HasKey then Lookup.  On failure, we would otherwise
            // throw an exception from Lookup.
            if (!_this.HasKey(key))
            {
                value = default(V);
                return(false);
            }

            try
            {
                value = _this.Lookup(key);
                return(true);
            }
            catch (Exception ex)  // Still may hit this case due to a race condition
            {
                if (__HResults.E_BOUNDS == ex._HResult)
                {
                    value = default(V);
                    return(false);
                }
                throw;
            }
        }
Esempio n. 2
0
        internal bool TryGetValue <K, V>(K key, out V value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            IMapView <K, V> mapView = JitHelpers.UnsafeCast <IMapView <K, V> >(this);

            if (!mapView.HasKey(key))
            {
                value = default(V);
                return(false);
            }
            bool result;

            try
            {
                value  = mapView.Lookup(key);
                result = true;
            }
            catch (Exception ex)
            {
                if (-2147483637 != ex._HResult)
                {
                    throw;
                }
                value  = default(V);
                result = false;
            }
            return(result);
        }
Esempio n. 3
0
        internal bool ContainsKey <K, V>(K key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            IMapView <K, V> mapView = JitHelpers.UnsafeCast <IMapView <K, V> >(this);

            return(mapView.HasKey(key));
        }
        internal bool ContainsKey <K, V>(K key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            IMapView <K, V> _this = Unsafe.As <IMapView <K, V> >(this);

            return(_this.HasKey(key));
        }
        internal bool TryGetValue <K, V>(K key, out V value)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            IMapView <K, V> _this = Unsafe.As <IMapView <K, V> >(this);

            // It may be faster to call HasKey then Lookup.  On failure, we would otherwise
            // throw an exception from Lookup.
            if (!_this.HasKey(key))
            {
                value = default !; // TODO-NULLABLE-GENERIC
        // bool TryGetValue(TKey key, out TValue value)
        internal bool TryGetValue <K, V>(K key, [MaybeNullWhen(false)] out V value) where K : notnull
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            IMapView <K, V> _this = Unsafe.As <IMapView <K, V> >(this);

            // It may be faster to call HasKey then Lookup.  On failure, we would otherwise
            // throw an exception from Lookup.
            if (!_this.HasKey(key))
            {
                value = default !;