Considera il seguente codice:
void Handler(object o, EventArgs e)
{
// I swear o is a string
string s = (string)o; // 1
//-OR-
string s = o as string; // 2
// -OR-
string s = o.ToString(); // 3
}
Qual è la differenza tra i tre tipi di casting (ok, il terzo non è un casting, ma ottieni l'intento). Quale dovrebbe essere preferito?
string s = Convert.ToString(o):; 5: string s = $"{o}"(o equivalentemente il string.Formatmodulo per C # precedente)