❔ Uno using c# console
basically as a school project we were asked to make a uno game code with certain rules, Me and my team did not know how to do a part of the code so we went straight to google and found the only way is to use the method .tryparse , i wanted to know if someone could check my code and see if there is a way to get rid of it.
thanks anyways!
9 Replies
$tryparse
The TryParse pattern is considered best practice of parsing data from a string:
- a TryParse method returns
true
or false
to inform you if it succeeded or not, so you can use it directly in a condition,
- since C# 7 you can declare a variable that will be used as an out
argument inline in an argument list,
- it forces you to check if the out
argument contains valid data afterwards,
Avoid: Convert.ToInt32 — it's a bad choice for parsing an int
. It exists only for backwards compatibility reasons and should be considered last resort. (Note: Convert does contain useful conversion methods: To/FromBase64String
, To/FromHexString
, ToString(X value, int toBase)
, ToX(string? value, int fromBase)
)
Avoid: int.Parse — you have to use a try
/catch
statement to handle invalid input, which is a less clean solution.
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__ is there any way that you could look at my code and see if this line is needed?
because i think my school intended on a other way
It's quicker if you share the relevant bit of code rather than asking to share the code first 🙂
@SinFluxx @Networking is a pain save me pls
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.