You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For a long time now, it was preferable to use int.Parse(string) over Convert.ToInt32(string). If you look at the implementation of ToInt32, you can see that it internally calls int.Parse(string), BUT returns 0 when input is null. This behaviour is rarely desirable. Even better than Parse is TryParse, since it is cleaner and performs better than handling exceptions with trycatch. Parse is of course better when it comes to writing a simple code snippet for a lecture.
In modern .NET (version 7+) there is IParsable<TSelf> interface that reinforces the suggested pattern even further.
My suggested changes are to reflect these facts in lecture notes (and in related code examples).
The only relevant code example I could find with VS search was in CodeDemonstrations.cs:
Console.WriteLine("Waiting for input (number)...");intinput=Convert.ToInt32(Console.ReadLine());Console.WriteLine($"Input: {input}");
So that's not too bad, but the lack of it in lecture notes is worse.
The text was updated successfully, but these errors were encountered:
For a long time now, it was preferable to use
int.Parse(string)
overConvert.ToInt32(string)
. If you look at the implementation of ToInt32, you can see that it internally callsint.Parse(string)
, BUT returns0
when input isnull
. This behaviour is rarely desirable. Even better thanParse
isTryParse
, since it is cleaner and performs better than handling exceptions withtry
catch
.Parse
is of course better when it comes to writing a simple code snippet for a lecture.In modern .NET (version 7+) there is
IParsable<TSelf>
interface that reinforces the suggested pattern even further.My suggested changes are to reflect these facts in lecture notes (and in related code examples).
The only relevant code example I could find with VS search was in
CodeDemonstrations.cs
:So that's not too bad, but the lack of it in lecture notes is worse.
The text was updated successfully, but these errors were encountered: