The difference between myData as a string, (string) myData and Convert.ToString (myData)

I was curious to know the difference between using a keyword as, casting, or using a function Convert.To_______().

After a short search, I found that the operator asonly performs link conversions and box transforms. What other differences exist.

I also noticed that the keyword is asvery rarely used, why this is so. Any of them has a performance advantage over others or just redundancy in the Framework.

+3
source share
3 answers

(cast) :

  • /
  • / Nullable-of-T

"as"

  • NULL-T-T

, test - , . , :

// is it a list?
IList list = obj as IList
if(list != null) {
    // specific code for lists
}

, , (), . ( ) .

Convert , ( , static. Parsse methods)

, Convert, . (cast) "as" .

+6

casting vs as: casting throws in exception, null, . .

: . , . , Convert.

+1

% 100 , , , , Convert,

.

+1

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


All Articles