A string is a char[]
, a string. I made an extension method:
public static char ToChar(this string value) {
if (string.IsNullOrEmpty(value)) throw new ArgumentException();
// retorna o primeiro caractere da string
// se não quiser usar o System.Linq, pode fazer "return value[0];"
return value.First();
}
This method will make:
"a".ToChar(); // char 'a'
"1".ToChar(); // char '1'
"".ToChar(); // ArgumentException foi disparado
"321".ToChar(); // char '3'
"xyz".ToChar(); // char 'x'
It always returns the first character. To use with integers, see:
1.ToString().ToChar(); // char '1'
Behold working at Ideone.
Have any answers solved what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?
– Maniero