使用预压缩键定义字典c#

时间:2015-09-22 08:50:21

标签: c# dictionary

我有一个班级Properties,我已经定义了这样的字典:

 public class Properties
    {
        public IDictionary<string, string> ExtendedProperties
        {
            get;
            set;
        }
    }

在字典中,总会有3个密钥表示NameNumberAge,可选择在运行时添加更多KeyValuePairs

我希望默认情况下将上述3个键存在于字典中,同时在我的代码中初始化它,以便我可以像这样直接使用它:

Properties objProps = new Properties();
objProps.ExtendedProperties["Name"] = "SomeName";

我知道我可以在我的代码中通过将KeyValuePair添加到字典中来实现这一点但我希望使用get-set直接在类中设置它以包含3个键。我找不到任何在课堂上做到这一点的解决方案。我调查了这个Creating dictionaries with predefined keys,但没有发现它令人满意。

我怎样才能做到这一点?

6 个答案:

答案 0 :(得分:4)

从C#6开始,您可以执行以下操作:

using System;
using System.Collections.Generic;

public class Properties
{
    public IDictionary<string, string> ExtendedProperties { get; set; }

    public Properties(string name, string number, string age) 
    {
        this.ExtendedProperties = new Dictionary<string, string>() 
        { 
            ["Name"] = name,
            ["Number"] = number,
            ["Age"] = age
        };
    }
}

如您所见,您需要在构造函数中定义它。

您可能还想使用一些很酷的功能:

public int this[int param]
{
    get { return array[param]; }
    set { array[param] = value; }
}

Documentation

如果你添加这样的东西,你可以new Properties()["Name"]

代码示例:

using System;
using System.Collections.Generic;

public class Properties
{
    private IDictionary<string, string> extendedProperties;

    public string this[string key] 
    {
        get { return extendedProperties[key]; }
        set { extendedProperties[key] = value; }        
    }

    public Properties() 
    {
        this.extendedProperties = new Dictionary<string, string>() 
        { 
            ["Name"] = "something",
            ["Number"] = "something",
            ["Age"] = "something"
        };
    }
}

答案 1 :(得分:1)

如何在构造函数中添加3个条目?

using System;
using System.Collections.Generic;

namespace My.Namespace
{
    public class Properties
    {
        public IDictionary<string, string> ExtendedProperties { get; set; }

        public Properties()
        {
            ExtendedProperties = new Dictionary<string, string>
            {
                ["Name"] = String.Empty,
                ["Number"] = String.Empty,
                ["Age"] = String.Empty
            };
        }
    }
}

答案 2 :(得分:1)

你可以这样做。

 public class Properties
 {
     public IDictionary<string, string> ExtendedProperties
     {
         get;
         set;
     }

     public Properties(string [] fields)
     {
         ExtendedProperties = new Dictionary<string, string> ();
         foreach(var s in fields)
         {
             ExtendedProperties.Add(s,string.Empty);
         }
     }
 }

用法:

Properties p = new Properties(new [] {"Name","Number", "Age"});

工作小提琴手code

答案 3 :(得分:1)

我会实现IDictionary<string, string>,因为使用其他键扩展更安全,更容易:(需要长课)

class Properties : IDictionary<string, string>
{
    private Dictionary<string, string> _staticProps;
    private Dictionary<string, string> _otherProps;

    public Properties()
    {
        _staticProps = new Dictionary<string, string>
        {
            {"Name", "" },
            {"Number", "" },
            {"Age", "" }
        };
        _otherProps = new Dictionary<string, string>();
    }

    public ICollection<string> Keys
    {
        get
        {
            return (ICollection<String>)_otherProps.Keys.Concat(_staticProps.Keys);
        }
    }

    public ICollection<string> Values
    {
        get
        {
            return (ICollection<String>)_otherProps.Values.Concat(_staticProps.Values);
        }
    }

    public int Count
    {
        get
        {
            return _otherProps.Count + _staticProps.Count;
        }
    }

    public bool IsReadOnly
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public string this[string key]
    {
        get
        {
            if (_otherProps.ContainsKey(key))
            {
                return _otherProps[key];
            }
            if(_staticProps.ContainsKey(key))
            {
                return _staticProps[key];
            }
            throw new KeyNotFoundException(key);
        }

        set
        {
            if (_otherProps.ContainsKey(key) || _staticProps.ContainsKey(key))
            {
                throw new ArgumentException("key exists: " + key);
            }
            _otherProps[key] = value;
        }
    }

    public bool ContainsKey(string key)
    {
        return _otherProps.ContainsKey(key) || _staticProps.ContainsKey(key);
    }

    public void Add(string key, string value)
    {
        _otherProps.Add(key, value);
    }

    public bool Remove(string key)
    {
        if (_staticProps.ContainsKey(key))
        {
            throw new ArgumentException("key is static, cannot be removed: " + key);
        }
        return _otherProps.Remove(key);
    }

    public bool TryGetValue(string key, out string value)
    {
        return _otherProps.TryGetValue(key, out value) || _staticProps.TryGetValue(key, out value);
    }

    public void Add(KeyValuePair<string, string> item)
    {
        if (_staticProps.ContainsKey(item.Key))
        {
            throw new ArgumentException("key exist an is static: " + item.Key);
        }
        _otherProps.Add(item.Key, item.Value);
    }

    public void Clear()
    {
        _otherProps.Clear();
        foreach (var key in _staticProps.Keys)
        {
            _staticProps[key] = string.Empty;
        }
    }

    public bool Contains(KeyValuePair<string, string> item)
    {
        return _otherProps.Contains(item) || _staticProps.Contains(item);
    }

    public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
    {           
        // define yourself how you want to handle arrayIndex between the two dictionaries
    }

    public bool Remove(KeyValuePair<string, string> item)
    {
        if (_staticProps.ContainsKey(item.Key))
        {
            throw new ArgumentException("key is static, cannot be removed: " + item.Key);
        }
        return _otherProps.Remove(item.Key);
    }

    public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
    {
        return _otherProps.Concat(_staticProps).GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return _otherProps.Concat(_staticProps).GetEnumerator();
    }
}

答案 4 :(得分:0)

  

我想让上面描述的3个键出现在   默认情况下,字典在我的代码中初始化,以便我可以   像这样直接使用它

你可以这样做:

 public class Properties
    {
        private Dictionary<string, string> extendedProperties = new Dictionary<string, string> ()
        {
                { "Name", "" },
                { "Number", "" },
                { "Age", "" },
        };

        public IDictionary<string, string> ExtendedProperties 
        {
            get { return extendedProperties; }
        }
    }

答案 5 :(得分:0)

我会将这个逻辑封装在属性本身中:

public class Properties
{
    private IDictionary<string, string> _extendedProperties;
    public IDictionary<string, string> ExtendedProperties
    {
        get
        {
            return
                _extendedProperties == null ?
                    new Dictionary<string, string>() { { "Name", "" }, { "Number", "" }, { "Age", "" } } :
                    _extendedProperties;
        }
        set 
        { 
            _extendedProperties = value; 
            //here you can also check if value misses those key to add them to _extendedProperties
        }
    }
}