Why does "how" not work with value types when "is"?

Operator

'as' does not work with type values, but 'is' does. why?

+3
source share
3 answers

Because the operator asreturns nullif the type does not match, and the type of the value cannot contain the value null.

for instance

double d = myVariable as double;

if myVariablenot double, d will be null, and this is not a suitable value for a double.

+7
source

Check this out: C # isandas

+3
source

as ( ).
as , , , .

int? i = 3;
double? d = (double?)i;

( ) #,

int? i = 3;
double? d = i as double?;

errors with 'Unable to convert type' int? ' double? ' by link conversion, box conversion, unboxing conversion, conversion conversion, or null type conversion "

0
source

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


All Articles