Why does ReSharper recommend using the keyword "as" instead of "is"?

I use Resharper8 and when I wrote

if (sender is Button) 

Resharperreturn the use of the keyword asand change it to:

Button button = sender as Button;
if (button != null)

Is there any special reason?

use aswhat is better than is? or is it asbetter in this case ?

+4
source share
4 answers

asperhaps better if the resulting expression is used later; isjust as good if not. For instance.

if (sender is Button) {
    Button button = (Button)sender;  // just use `as`, as suggested
    button.Push();

(I thought ReSharper only gave a warning / hint in the case where the duplicate can be deleted, for example this.)

In doing so, I use the structure

Button button;
if ((button = sender as Button) != null) {
    button.Push();

, ( ) - as #/ReSharper [] .


. :

"" CLR - "/()", " ", , . " "" " .

: "as" "" ?, .

+4

, , as : . , ,

+3

"as" . , "is" , , .

, , , null, .

Eric Liperts Is ? strong >

CLR isstst, . , , . , de jure is is is, , - .

"" "" ?

:

"" "

is, cast, :

  • true, Type cast

, , CLR , . , , step. is.

+3

. , , , - . , , if . .

0

Source: https://habr.com/ru/post/1542315/


All Articles