Cast classic
private void textBox1_Leave(object sender, EventArgs e) {
TextBox textBoxTemp = (TextBox)sender;
MessageBox.Show("Você digitou: " + textBoxTemp.Text);
}
Are you absolutely sure sender
is the type TextBox
? As I know the default Windows Forms you are using I would say yes, it is. So this "conversion" will not fail, it is certain that it is possible. Any situation you will know that it will not fail you can use it. In many cases the cost is zero.
But I will stress that you have to be sure, it is not always possible to guarantee this.
Know that if there is an error in the conversion attempt the application will make an exception and possibly break. And this is good. It was a programming error somewhere even. You must fix the error. You should not capture the exception and try to tidy up. Programming errors should be solved by the programmer, not by code. One of the possible solutions is not to use the cast in this way.
Operator as
That’s where the operator comes in as
. The use of it implies that you’re not quite sure that the operation will work. Then we can consider that it works as an attempt and if it fails the object that should receive the "conversion" will receive the null value. So always after using the as
it is necessary to check if the object is not null. Then 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 choose 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 don’t want it to go wrong, it might be a good option. It does not seem to be the case in this example since it will show the message "You typed: " and nothing else, will not indicate problem, nothing.
The cost of the operator as
plus checking whether the operation was successful is usually the same as using the cast in most situations. And when there is difference it is nothing absurd. Anyway you should use whatever is right for the situation.
The as
cannot make conversions into types by value directly since they cannot have null result. However, it is possible to use an overridable type to get the same result.
It also cannot 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 the cast traditional.
Common mistake
Don’t exaggerate 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" launching the exception. It is wrong. This code is comparing whether the type is compatible once in the if
and then compares again inside the cast.
Worse, in some cases there may be a race condition since between the first check and the actual conversion the state of the object may have changed.
Pattern matching
In C# 7 it is possible to create the variable conditionally with Pattern matching without needing the cast:
private void textBox1_Leave(object sender, EventArgs e) {
if (textBoxTemp isTextBox textBoxTemp) {
MessageBox.Show("Você digitou: " + textBoxTemp.Text);
}
}
Completion
Note that operators have different semantics, they are used for different situations. When using one or the other should make you think about the design application, you must understand why you are using one or the other. It’s not just a choice of what works or not.
Partially inspired response in that OS response.
I put in the Github for future reference.
http://blogs.msdn.com/b/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx
– PauloHDSousa