In this fourth installment of C# Basics, let’s take a look at how you handle multiple choice questions in your code. When you need to decide on a course of action based on the multiple possible values a variable may have, you have three essential choices.
- if | else if … | else
- switch
- Dictionary<K,T>
The code below shows you an example of each:
public class DownloadViewModel
{
public Byte[] Contents { get; set; }
public string FileName { get; set; }
public string ContentType1
{
get
{
string ext = Path.GetExtension(FileName).ToLower();
if (ext == ".pdf")
return "application/pdf";
else if (ext == ".txt")
return "application/txt";
else
return "application/octet-stream";
}
}
public string ContentType2
{
get
{
switch (Path.GetExtension(FileName).ToLower())
{
case ".pdf":
return "application/pdf";
case ".txt":
return "application/txt";
default:
return "application/octet-stream";
}
}
}
public string ContentType3
{
get
{
if (map.Count == 0) LoadMap();
string val = string.Empty;
if (!map.TryGetValue(Path.GetExtension(FileName).ToLower(), out val))
{
val = "application/octet-stream";
}
return val;
}
}
private void LoadMap()
{
map.Add(".pdf", "application/pdf");
map.Add(".txt", "application/txt");
map.Add("*.*", "application/octet-stream");
}
private Dictionary<string, string> map = new Dictionary<string, string>();
}
So how do you pick which one to use? For me, the choice is easy. If I have only a few possible values that are not likely to change, I’ll use the if|else if|else construct. If I have a fair number (more than 3 and less than 16) and these values are not likely to change, I’ll use the switch statement. But if the values are likely to change or be data driven or if the number of values is greater than 15, I prefer to use a Dictionary.
Another highly valuable use of the Dictionary is when the values will kick off some action that may be lengthy or complicated. In that case, I prefer a Dictionary<T, ActionForT>. I can then retrieve the ActionForT and call the Execute method. Like this:
public class DownloadModel
{
public Byte[] Contents { get; set; }
public string FileName { get; set; }
public string ContentType
{
get
{
string ext = Path.GetExtension(FileName).ToLower();
if (ext == ".pdf")
return "application/pdf";
else if (ext == ".txt")
return "application/txt";
else
return "application/octet-stream";
}
}
public void Save()
{
if (map.Count == 0) LoadMap();
Action action = null;
if (map.TryGetValue(Path.GetExtension(FileName).ToLower(), out action))
{
action(); //execute the action stored in our map
}
else
{
map["*.*"](); //execute the default action
}
}
private void SavePdf()
{
throw new NotImplementedException();
}
private void SaveTxt()
{
throw new NotImplementedException();
}
private void SaveOther()
{
throw new NotImplementedException();
}
private void LoadMap()
{
map.Add(".pdf", new Action(SavePdf));
map.Add(".txt", new Action(SaveTxt));
map.Add("*.*", new Action(SaveOther));
}
private Dictionary<string, Action> map = new Dictionary<string, Action>();
}