Cast classic
private void textBox1_Leave(object sender, EventArgs e) {
TextBox textBoxTemp = (TextBox)sender;
MessageBox.Show("Você digitou: " + textBoxTemp.Text);
}
Are you absolutely sure that sender
is of type TextBox
? As I know the pattern of Windows Forms you are using I would say yes, it is. Therefore this "conversion" will not fail, it is certain that it is possible. Whatever situation you know will not fail you can use it. In many cases the cost is zero.
But I will reinforce that you have to be sure, it is not always possible to guarantee this.
Please note that if there is an error in the conversion attempt the application will throw an exception and possibly break. And this is good. It was a programming error somewhere. You should fix the bug. You should not catch the exception and try to fix it . Programming errors should be solved by the programmer, not the code. One possible solution is to not use the cast operator like this.
Operator as
This is where the as
operator enters. The use of it implies that you are not very sure that the operation will work. Then we can consider that it works as an attempt and if it fails the object it should receive the "conversion" will receive the null value. That is why always after using as
you must check if the object is not null. So you would:
private void textBox1_Leave(object sender, EventArgs e) {
TextBox textBoxTemp = sender as TextBox;
if (textBoxTemp != null) {
MessageBox.Show("Você digitou: " + textBoxTemp.Text);
}
}
Operator null-propagating
In C # 6 you can opt for this code:
private void textBox1_Leave(object sender, EventArgs e) {
TextBox textBoxTemp = sender as TextBox;
MessageBox.Show("Você digitou: " + textBoxTemp?.Text);
}
Not that the result is the same, but if you just do not want it to go wrong, it might be a good choice. It does not seem to be the case in this example since it will display the message "You typed:" and nothing else, it will not indicate problem, nothing.
Cost of operator as
plus verification that the operation was successful is often the same as using cast in most situations. And when there is difference, it is not absurd. Anyway you should use whatever is right for the situation.
as
can not directly convert type-valued types since they can not have null results. However you can use a nullable type to get the same result.
It also can not do user-defined conversions (even those defined within .Net), only language-defined conversions are possible. These conversions usually require some processing and can only be done with traditional cast .
Common error
Do not make exaggerations how to do this:
if (sender is TextBox) {
TextBox sender = (TextBox) textBoxTemp;
//faz algo aqui
}
It is common to see this in some code to "avoid" throwing the exception. This is wrong. This code is comparing if the type is compatible once in if
and then compares again within cast .
Worse, in some cases there may be a race since you enter the first check and the actual conversion status of the object may have changed.
Conclusion
Note that operators have different semantics, they are used for different situations. When using one or the other should make you think of the design of the application, you should understand why you are using one or the other. It's not just a choice of what works or not.
Partially inspired response in this SO response .