问题
How do you define a getter and setter for complex data types such as a dictionary?
public Dictionary<string, string> Users
{
get
{
return m_Users;
}
set
{
m_Users = value;
}
}
This returns the entire dictionary? Can you write the setter to look and see if a specific key-value pair exists and then if it doesn't, add it. Else update the current key value pair? For the get, can you return a specific key-value pair instead of the whole dictionary?
回答1:
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.
回答2:
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";
回答3:
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. :)
回答4:
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.
回答5:
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.
回答6:
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;
}
来源:https://stackoverflow.com/questions/11583595/how-to-write-a-getter-and-setter-for-a-dictionary