Skip to content

User Input in .NET 7.0

User Input in .NET 7.0
Become a developer with our complete learning paths
Become a developer with our complete learning paths

User Input in .NET 7.0

In this article, we’ll discuss how to receive User Input in .NET 7.0 using the Console class. We’ll also go over some best practices and considerations for user input in C#.

The Console class is a static class in the System namespace that provides methods for interacting with the console window. One of the key methods we’ll be using is the Console.ReadLine() method, which reads a line of text input from the user.


Let´s get the user´s input

To illustrate how to use Console.ReadLine(), let’s start with a simple example:

In this example, we prompt the user to enter their name using Console.Write(), and then read their input using Console.ReadLine(). We then use the input to construct a greeting message, which we output to the console using Console.WriteLine().

One important thing to note is that Console.ReadLine() reads the entire line of input from the user, including any whitespace. This means that if the user types in “John Doe”, the input will include the space between “John” and “Doe”.

If you only want to read a single word from the user, you can use the Console.Read() method instead. This method reads a single character from the console input stream and returns it as an integer value.

In this example, we read a single character from the console input stream using Console.Read(), and then convert it to a char type using an explicit cast. We then use the char value to construct a message that outputs the first letter of the user’s input.

It’s worth noting that Console.Read() returns an integer value rather than a char value because it can also return special control characters such as the newline character or the backspace character. This is something to keep in mind if you’re working with input that might include control characters.


Input validation

Another consideration when working with user input is input validation. Whenever you’re accepting input from a user, you should always assume that the input may be malicious or unintentionally incorrect, and validate it accordingly.

One common technique for input validation is to use regular expressions to check that the input matches a particular pattern. For example, if you’re asking the user to enter an email address, you might use a regular expression to ensure that the input is a valid email address.

In this example, we use the Regex.IsMatch() method to check that the user’s input matches a regular expression pattern for a valid email address. If the input matches the pattern, we output a message indicating that the input is valid. If the input does not match the pattern, we output a message indicating that the input is invalid.


Checking the user´s input for harmful characters

It’s also a good idea to sanitize user input to remove any potentially harmful characters or strings. For example, if you’re accepting input for a database query, you might use a sanitization function to remove any quotes or semicolons that could be used to inject SQL code into the query.

In this example, we define a SanitizeInput() method that removes single quotes and semicolons from the input string. We then call this method on the user’s input before using it in a database query. This helps to prevent SQL injection attacks.


Handling unexpected behaviours

Finally, it’s worth noting that user input can sometimes cause unexpected behavior in your program. For example, if you’re using Console.ReadLine() to get a number from the user, and the user enters a non-numeric value, your program will likely throw an exception.

One way to handle this is to use the int.TryParse() method, which attempts to convert a string to an integer value. If the conversion is successful, the method returns true and sets an output parameter to the converted value. If the conversion fails, the method returns false and sets the output parameter to 0.

In this example, we use int.TryParse() to attempt to convert the user’s input to an integer value. If the conversion is successful, we output the square of the number. If the conversion fails, we output an error message.


Conclusion: User Input in .NET 7.0

To summarize, user input in C# can be received using the Console class, and validated and sanitized using regular expressions and custom sanitization functions. It’s important to handle unexpected input and exceptions, and to always assume that user input may be malicious or unintentionally incorrect. By following these best practices, you can ensure that your C# programs are robust and secure.

If you want to skyrocket your C# career, check out our powerful ASP.NET FULL-STACK WEB DEVELOPMENT COURSE, which also covers test-driven development and C# software architecture.


Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide