Melhor usar HashTable ou alternar maiúsculas e minúsculas
Não tenho certeza de qual é o melhor. Eu preciso analisar cada caractere de uma string de entrada e obter uma string de substituição para o caractere. Para alguns objetos, todos os caracteres alfanuméricos são permitidos, portanto, usar switch / case causará muito código e reduzirá a legibilidade e a manutenção, mas eu posso usar um método estático. O uso de uma HashTable também exige muito código
Usando o método estático:
private static string EncodeChar(char c)
{
var symbols = string.Empty;
switch (c)
{
case '0':
symbols = "Test";
break;
case '1':
symbols = "Hello";
break;
[...]
}
symbols;
}
Usando HashTable:
private static Hashtable table = CreateTable();
private static Hashtable CreateTable()
{
var table = new HashTable();
table.Add('0',"Test");
table.Add('1', "Hello");
[...]
return table;
}
private static string EncodeChar(char c)
{
return table.ContainsKey(c) ? table[c].ToString() : string.Empty;
}
O método de codificação:
public void Encode()
{
string output = string.Empty;
for (int i = 1; i < Data.Length; i++)
{
output = string.Concat(output, EncodeChar(Data[i]));
}
EncodedData = output;
}
Quais são as vantagens / desvantagens relacionadas ao desempenho e à alocação de memória?