3
Reply

Why should not use AS operator for the typecating in Value type? float c = 20; e.g. int a = c as int; its wrong. Why?

Gul Md Ershad

Gul Md Ershad

Dec 02, 2014
2k
1

    As operator set null value if it is unable to typecast. Value type can't accept null except string. So, as operator is not being used with value type in c#.

    Gul Md Ershad
    December 02, 2014
    1

    As operator is used mainly with References type or null able type because if conversion is not possible then it return NULL value but int can not store it so we can not us "AS" operator with non-nullable value type

    Note that 'as' operator relies on implicit typecasting. Using as implies that you actually know the type of the variable at the compile time (or can assert that it will be what you expect). Rest is correctly mentioned by Gul Md Ershad.

    Vaibhav Singh
    March 11, 2015
    0