ReadOnlyDictionary.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / ReadOnlyDictionary.cs / 1 / ReadOnlyDictionary.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel
{ 
    using System;
    using System.Collections; 
    using System.Collections.Generic; 
    using System.Runtime.Serialization;
 
    internal sealed class ReadOnlyDictionary : IDictionary
    {
        IDictionary dictionary;
        bool isFixedSize; 

        internal ReadOnlyDictionary(IDictionary dictionary) 
            : this(dictionary, true) 
        {
            // empty 
        }

        internal ReadOnlyDictionary(IDictionary dictionary, bool makeCopy)
        { 
            if (makeCopy)
                this.dictionary = new Dictionary(dictionary); 
            else 
                this.dictionary = dictionary;
 
            this.isFixedSize = makeCopy;
        }

        public V this[K key] 
        {
            get 
            { 
                return this.dictionary[key];
            } 
            set
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
            } 
        }
 
        public ICollection Keys 
        {
            get 
            {
                return this.dictionary.Keys;
            }
        } 

        public ICollection Values 
        { 
            get
            { 
                return this.dictionary.Values;
            }
        }
 
        public int Count
        { 
            get 
            {
                return this.dictionary.Count; 
            }
        }

        public bool ContainsKey(K key) 
        {
            return this.dictionary.ContainsKey(key); 
        } 

        public void Add(K key, V value) 
        {
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
        }
 
        public void Clear()
        { 
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly))); 
        }
 
        public bool IsReadOnly
        {
            get
            { 
                return true;
            } 
        } 

        public bool IsFixedSize 
        {
            get
            {
                return this.isFixedSize; 
            }
        } 
 
        public bool TryGetValue(K key, out V value)
        { 
            return this.dictionary.TryGetValue(key, out value);
        }

        void ICollection>.Add(KeyValuePair keyValuePair) 
        {
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly))); 
        } 

        bool ICollection>.Contains(KeyValuePair keyValuePair) 
        {
            return (this.ContainsKey(keyValuePair.Key) && this[keyValuePair.Key].Equals(keyValuePair.Value));
        }
 
        bool ICollection>.Remove(KeyValuePair keyValuePair)
        { 
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly))); 
        }
 
        public bool Remove(K key)
        {
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ObjectIsReadOnly)));
        } 

        void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) 
        { 
            ((IDictionary)this.dictionary).CopyTo(array, arrayIndex);
        } 

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable>)this).GetEnumerator(); 
        }
 
        IEnumerator> IEnumerable>.GetEnumerator() 
        {
            return ((IDictionary)this.dictionary).GetEnumerator(); 
        }
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK