Avatar ·

How to convert a string to an integer (int) in C#?

Hello everyone! I just started learning C# and ran into a problem. I have a string that I get from the user via `Console.ReadLine()`, for example, "123". I need to perform mathematical operations on it, such as adding it to another number. But the compiler complains and says that you cannot add `string` and `int`.\n\nHere is my code that doesn't work:\n\n```csharp\nstring userInput = Console.ReadLine(); // user enters "25"\nint result = userInput + 10; // Error: cannot add string and int\nConsole.WriteLine(result);\n```\n\nI understand that a string and a number are different data types, and they need to be converted somehow. But I don't know how to do it correctly. I've seen many different methods on the internet: `Convert.ToInt32()`, `int.Parse()`, `TryParse()`. Which one is best to use?