How to write a getter and setter for a Dictionary?

前端 未结 6 1673
暗喜
暗喜 2020-12-17 09:09

How do you define a getter and setter for complex data types such as a dictionary?

public Dictionary Users
{
    get
    {
        retu         


        
相关标签:
6条回答
  • 2020-12-17 09:47

    You won't be able to do that with a property. You'll need to use methods for that, or add an indexer to your class. The get method can't accept a parameter (the key).

    Another option, if you want someone to be able to easily add/remove keys to the dictionary but prevent them from setting an entirely new one would be to make the property a read-only property that returns a dictionary created in the constructor. It would be less flexible then adding get/set methods, but in common, simple cases it can do just fine.

    0 讨论(0)
  • 2020-12-17 09:48

    Use an indexer property (MSDN):

    public class YourClass
    {
        private readonly IDictionary<string, string> _yourDictionary = new Dictionary<string, string>();
    
        public string this[string key]
        {
            // returns value if exists
            get { return _yourDictionary[key]; }
    
            // updates if exists, adds if doesn't exist
            set { _yourDictionary[key] = value; }
        }
    }
    

    Then use like:

    var test = new YourClass();
    test["Item1"] = "Value1";
    
    0 讨论(0)
  • 2020-12-17 10:03

    It looks like you want an "named indexer". Here's (my) one way to accomplish that using C#.

    My approach exposes a property that returns an object (with a default indexer) which will perform the indexing into the appropriate field given the lambdas to do it.

    There are reasons you may or not want to use this method, but I'll leave that to you. :)

    0 讨论(0)
  • 2020-12-17 10:07

    It is not possible to do it in a way that would involve only properties. You theoretically could write a setter, but for a getter, you would need to specify a key that you want to retrieve. That is impossible since properties do not accept parameters. Natural way to accomplish what you want would be to use methods:

    private Dictionary<string, string> users = new Dictionary<string, string>();
    
    public void Set(string key, string value)
    {
        if (users.ContainsKey(key))
        {
            users[key] = value;
        }
        else
        {
            users.Add(key, value);
        }
    }
    
    public string Get(string key)
    {
        string result = null;
    
        if (users.ContainsKey(key))
        {
            result = users[key];
        }
    
        return result;
    }
    

    Alternatively, as others have already said, you could use indexers, but I've always found them a little cumbersome. But I guess it's just a matter of personal preference.

    And just for the sake of completeness, this is how a setter could look like, although it's highly unusual and counter-intuitive to have such a property:

    public KeyValuePair<string, string> Users
    {
        set
        {
            Set(value.Key, value.Value);
        }
    }
    

    Internally, it uses the Set method from my previous snippet.

    0 讨论(0)
  • 2020-12-17 10:07
    Dictionary<string, string> param = new Dictionary<string, string>();
    
    public void SetYourParameter(string parametrName, string paramValue)
    {
        param[parametrName] = paramValue;            
    }
    
    public string GetYourParameter(string parametrName)
    {
        // ContainKey ---> It returns value if the key was found
        if( param.ContainsKey(parametrName))
            return param[parametrName];
        else
            return null;
    }
    
    0 讨论(0)
  • 2020-12-17 10:12

    It is possible to do so with the setter but highly unrecommended, and is completely impossible with the getter as it takes no parameter to determine what to get.

    For the setter you would have to pass a Dictionary<string, string> with a single pair but it goes against what you would expect the getter/setter to usually do and completely stops you setting the entire Dictionary.

    A much better way is to use a pair of methods which you can name Get and Set if you so desire.

    0 讨论(0)
提交回复
热议问题