C# (Csharp) interview questions What is the difference between convert.tostring and tostring ()

C# (Csharp) interview questions What is the difference between convert.tostring and tostring ()


Now this is the very fundamental question what is the difference between convert.tostring and tostring function now is question is fundamental the answer is simple but many of the developers are not aware of the difference so basically developers use both of these functions interchangeably without going of the differences now the basic difference between both of these functions are that the convert are tostring handles nulls while the tostring function does not handle null so in other words when you have a string value which having null and if you give you to convert.tostring you will not get by error but when you give to the tostring function it will through of exception saying that is null exception so lets start to see a small demonstration in order to understand if this answer right or wrong now in order to demonstrate if your answer is right or wrong what i have done is i have created a simple window form here and on this windows form i have a simple button and in the button what i have done is i have created a string with with a null value and i am trying to use convert.Tostring so let me just do a ctrl F5 here and let me go and run this this form and let me click on the button when i click on the button i am not getting any errors so in other words this code is getting executed properly now let me go and close this form and let me use the tostring function so let me go here and say convert this use the tostring function and now let me again do a ctrl F5 and let me run this now you can see that i am getting a error here and if you go closely will see that the error is of nullReferenceException so in other words what the interviewer is expecting from you for this question is convert.Tostring handles null while the tostring function does not handle nulls now this is the very fundamental question and from the interviewers except every developer should answer these kind of fundamental questions now i am sure that practical professionals or developers will have the own argument that how does question judge a developer experience well but aa oo so you know thinking from the same aspect what i will suggest is basically go and prepare other dotnet basic aspect like you know the global assembly cache garbage collector intermediate language code boxing and unboxing etc so what i would suggest is go to the dotnet fundamental section and you can go through the question and answer series of IL code, CLR, CLS JIIT code access security garbage collector like the dispose pattern and the finalise pattern which people ask about a strong references weak references delay sign in when you go to use delay sign in global assembly cache if you have multiple version of how do you handle it delegate events etc etc so probably you can go to this section and prepare more on other dotnet fundamentals so that basically you do not fell in answering fundamental questions Thank You very much…..

12 thoughts on “C# (Csharp) interview questions What is the difference between convert.tostring and tostring ()

  • On a reference type, Convert.ToString attempts to call IConvertible.ToString, then IFormattable.ToString, then the regular ToString if not null, and returns String.Empty if the reference is null.

    If the parameter is a String (overload), it just returns the parameter.

  • @kodefuguru As a author of the video i would like to defend myself. The video was made from the aspect of .NET interview. The above note what you have put is absolutely genius. Its good for knowledge not for interview purpose. It can confuse further the interviewer on some other path , probably leading more confusions. Unless the interviewer is really smart to understand what the candidate speaking.

    But Yes, you have made a indepth analysis . Thanks.

  • You are correct, the in-depth knowledge isn't something most would expect or require. The deeper meaning behind your video is that you can't call instance methods if the instance is null, but you can call a helper method with the variable as an argument. Keep up the good work preparing people!

  • @kodefuguru haaah i just looked at your videos. Sorry they are pathetic. i saw one of your video i did not find it worth. I like this video , do not comment just because you want some cheap publicity. This is a great channel with more 200o subscribers as compared to your channel which is hardly 3 subscribers must be just your family members.

    Sorry i got a job because of this channel and i do not care of people like you. love DNF video.

  • @surajfrommumbai The points given by kodefu is nice , please do not disrespect , whatsever it is. I will need to delete all your messages on our channel.

  • I have a question. At the first place, why should we convert a string variable to a string.As in this ex. str is a string and again why should we convert it to string again?Whats the point?? So, i didn't get what is the plus point of using Convert.ToString() against .tostring() . In no scenarios we pass string variable to Convert.ToString() or .tostring() to be converted to string again.Whats d point!!

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *