将字典作为参数传递

时间:2018-10-26 09:03:00

标签: c# dictionary

  

设置

假设您有以下代码

public Dictionary<string,string> BuildSettings(){
    var result = new Dictionary<string,string>();

    result.Add("key1","value1");
    result.Add("key2","value2");
    //And so on...
    //Add a LOT more items to result

    return result;
}
  

问题

因为字典中有很多项目,所以我想将它们拆分。 但是...将字典传递给子方法(并将其返回给父方法)的“干净代码”方式是什么?以下选项的优点和缺点是什么?

  

可能的答案

选项1 :作为参数传递并返回字典

public Dictionary<string,string> BuildSettings(){
    var result = new Dictionary<string,string>();

    result = SubMethod1(result);
    result = SubMethod2(result);
    //And so on...

    return result;
}
public Dictionary<string,string> SubMethod1(Dictionary<string,string> result){
    result.Add("key1","value1");
    result.Add("key2","value2");
    //And so on...

    return result;
}

选项2 :作为参数->最流行的答案

传递
public Dictionary<string,string> BuildSettings(){
    var result = new Dictionary<string,string>();

    SubMethod1(result);
    SubMethod2(result);
    //And so on...

    return result;
}
public void SubMethod1(Dictionary<string,string> result){
    result.Add("key1","value1");
    result.Add("key2","value2");
    //And so on...
}

选项3 :以参数“ ref”传递以指示字典中的更改

public Dictionary<string,string> BuildSettings(){
    var result = new Dictionary<string,string>();

    SubMethod1(ref result);
    SubMethod2(ref result);
    //And so on...

    return result;
}

public void SubMethod1(ref Dictionary<string,string> result){
    result.Add("key1","value1");
    result.Add("key2","value2");
    //And so on...
}

选项4 :返回Dictionary的新实例并合并字典

public Dictionary<string,string> BuildSettings(){
    var result = new Dictionary<string,string>();

    foreach(var pair in SubMethod1()){
        result.Add(pair.Key, pair.Value);
    }
    foreach(var pair in SubMethod2()){
        result.Add(pair.Key, pair.Value);
    }
    //And so on...

    return result;
}

public Dictionary<string,string> SubMethod1(){
    var result = new Dictionary<string,string>();

    result.Add("key1","value1");
    result.Add("key2","value2");
    //And so on...

    return result;
}

选项5 :扩展方法

public Dictionary<string,string> BuildSettings(){
    var result = new Dictionary<string,string>();

    result.SubMethod1();
    result.SubMethod2();
    //And so on...

    return result;
}

public Dictionary<string,string> SubMethod1(this Dictionary<string,string> result){
    result.Add("key1","value1");
    result.Add("key2","value2");
    //And so on...

    return result;
}

选项6 :将字典作为类中的字段

private Dictionary<string,string> _settings = new Dictionary<string,string>();

public Dictionary<string,string> BuildSettings(){

    SubMethod1();
    SubMethod2();
    //And so on...

    return settings;
}

public void SubMethod1(){
    settings.Add("key1","value1");
    settings.Add("key2","value2");
    //And so on...
}

选项7 :还有其他建议吗?

  

结果

我认为选项2最好,就像这里的大多数人所说的那样。 另外,我认为添加一个好的方法名称也可以帮助解决这个问题。

所以这应该是代码(在我看来):

public Dictionary<string,string> BuildSettings(){
    var result = new Dictionary<string,string>();

    AddUserSettingsTo(result);
    AddCompanySettingsTo(result);
    //And so on...

    return result;
}

public void AddUserSettingsTo(Dictionary<string,string> result){
    result.Add("key1","value1");
    result.Add("key2","value2");
    //And so on...
}

2 个答案:

答案 0 :(得分:1)

如果键值对在编译时都是常量,则可以将它们全部写入JSON文件:

{"key1": "value1", "key2": "value2", ...}

并使用JSON.NET将其转换为字典。

string jsonFile = File.ReadAllLines(...);
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonFile);
return dict; // you probably want to cache this. It may be to inefficient to read the file every time you call BuildSettings()

如果KVP不是常数,我认为选项2是最好的选择。或者,为Dictionary<string, string>创建扩展方法,以便执行此操作:

result.AddBuildSettingsPart1();
result.AddBuildSettingsPart2();
result.AddBuildSettingsPart3();

答案 1 :(得分:-3)

只需将接口作为参数传递即可:

public Dictionary<string,string> BuildSettings(){
  var result = new Dictionary<string,string>();

  SubMethod1(result);
  SubMethod2(result);

  return result;
    }

public void SubMethod1(IDictionary<string,string> dictionary)
{
dictionary.Add("key1","value1");
dictionary.Add("key2","value2");
}
相关问题