From 0914d32a5a460ced3bbb035db60b3ad1284d68eb Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Mon, 20 Mar 2023 12:37:59 +0200 Subject: [PATCH] Make strings non-nullable by using different variables to just check input --- Lesson_2/Task_1/Program.cs | 10 +++++++--- Lesson_2/Task_3/Program.cs | 9 ++++++--- Lesson_2/Task_4/Program.cs | 9 ++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Lesson_2/Task_1/Program.cs b/Lesson_2/Task_1/Program.cs index c548bb6..9d26544 100644 --- a/Lesson_2/Task_1/Program.cs +++ b/Lesson_2/Task_1/Program.cs @@ -10,17 +10,21 @@ List products = new List(); for (int i = 0; i < 10; i++) { - string? name; + string name; float price; while (true) { Console.Write($"Enter product {i + 1} name: "); - name = Console.ReadLine(); - if (name != null) + string? input = Console.ReadLine(); + if (!string.IsNullOrEmpty(input)) + { + name = input; break; + } } + while (true) { Console.Write($"Enter product {i + 1} price: "); diff --git a/Lesson_2/Task_3/Program.cs b/Lesson_2/Task_3/Program.cs index 00715a8..c4df476 100644 --- a/Lesson_2/Task_3/Program.cs +++ b/Lesson_2/Task_3/Program.cs @@ -20,14 +20,17 @@ List customers = new List() new Customer("Alex", 77, "Street 10") }; -string? name; +string name; while (true) { Console.Write("Enter name: "); - name = Console.ReadLine(); - if (name != null) + string? input = Console.ReadLine(); + if (!string.IsNullOrEmpty(input)) + { + name = input; break; + } } var selected = from customer in customers where customer.Name == name select customer; diff --git a/Lesson_2/Task_4/Program.cs b/Lesson_2/Task_4/Program.cs index b7a9489..88f45fb 100644 --- a/Lesson_2/Task_4/Program.cs +++ b/Lesson_2/Task_4/Program.cs @@ -21,14 +21,17 @@ List cars = new List() new Car("789GHI", 3), }; -string? number; +string number; while (true) { Console.Write("Enter number: "); - number = Console.ReadLine(); - if (number != null) + string? input = Console.ReadLine(); + if (!string.IsNullOrEmpty(input)) + { + number = input; break; + } } var selected = owners.FirstOrDefault(owner => cars.Any(car => car.Number == number && car.OwnerId == owner.Id));