Both of these are accepted by the compiler:
ssMinnow = listStrLineElements[VESSEL_TO_AVOID].ToString();
ssMinnow = listStrLineElements[VESSEL_TO_AVOID];
Is one way preferable to the other? ToString() or not ToString(), that is the question.
It is not only redundant, but also is dangerous: if listStrLineElements[VESSEL_TO_AVOID]
happens to be null
, your application is going to throw an exception if you use ToString()
; without ToString()
, it would simply assign null
to ssMinnow
.
If listStrLineElements[VESSEL_TO_AVOID]
returns a string, then yes, it is redundant. if it returns some other type, then no, it is not redundant.
in General your don't need to invoke the ToString()
method is the object type returned is already a String
.
in your example we cannot tell that as ssMinnow
does not show the declaration type :I assume you have used var
keyword which will work with both of them or listStrLineElements[VESSEL_TO_AVOID]
returns already a String
Simply redundant. I prefer to leave off ToString() where its not needed but its a judgement call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With