ToString() vs as String | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

ToString() vs as String

What is the difference between .ToString() and as string

1st Apr 2017, 4:32 AM
John Devaule
1 Answer
+ 7
First, look at this : string y = x as String; If you have cast operators in this code, it will be ignored. Also, if x is not an instance of a string, y will become null. Meaning that no exceptions will be thrown during code runtime. string y = x.ToString(); So you would use this instead if you have cast operators, etc. But sometimes if it can't convert x to a string, it may throw exceptions.
1st Apr 2017, 4:36 AM
Wen Qin
Wen Qin - avatar