I've been trying to mull over a question I am not sure how to solve and it probably has a simple solution however I am trying to work out a simple solution.The following scenario is as such. Let's say we have the following variables.string username;string password;string domain;string location;and I want to run the same function across themvoid AssignValueFromConfigFile() {    username = ConfigurationManager.AppSettings[username];    if (username == null)        throw new ValueNotFound(username);    password = ConfigurationManager.AppSettings[password];}..... etc for each oneThis is obviously really tedious and the wrong way to do this. We can make this work in a dictionary if we had: var values = new Dictionary<string,string>() { {username,},{password,},{domain,},{location,}}void AssignValueFromConfigFile(){    foreach(var val in values)     {        val.Value = ConfigurationManager.AppSettings[val.Key];        if (val.Value == null)            throw new ValueNotFound(val.Value);    }}But then we lose the ability to call the local variables and have to know what they are in the dictionary. How is this achieved?
Assigning local variables in a bulk way
c#;refactoring
There's no need for a dictionary (unless you want one, of course). You could assign each variable by reference, e.g.void AssignValueFromConfigFile(string name, ref string value) {    value = ConfigurationManager.AppSettings[name];    if(value == null) {        throw new ValueNotFound(name);    }}    string username;    string password;    string domain;    string location;    AssignValueFromConfigFile(username, ref username);    AssignValueFromConfigFile(password, ref password);    AssignValueFromConfigFile(domain, ref domain);    AssignValueFromConfigFile(location, ref location);However, a much neater solution would be to apply the command/query separation pattern, e.g.string GetValueFromConfigFile(string name) {    var value = ConfigurationManager.AppSettings[name];    if(value == null) {        throw new ValueNotFound(name);    }    return value;}    var username = GetValueFromConfigFile(username);    var password = GetValueFromConfigFile(password);    var domain = GetValueFromConfigFile(domain);    var location = GetValueFromConfigFile(location);You have one function that reads the data and guarantees that it is not null. The calling code then decides what to do with the data.