0

My code works properly but every time when i run the code I have warning which I can not understand. I run the code in my linux terminal it says: Converting null literal or possible null value to non-nullable type Is it normal? Or should I do something? Here is my code:

namespace test2;
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Choose option: \n1. +\n2. -\n3. *\n4. /");
        int num = Convert.ToInt32(Console.ReadLine());
        string ext = Console.ReadLine();
        if(num == 1){
            while(true){
                Console.WriteLine("Enter numbers or tap Q to exit:");
            int x = Convert.ToInt32(Console.ReadLine());
            int y = Convert.ToInt32(Console.ReadLine());
            int z = x + y;
            Console.WriteLine("{0}+{1}={2}", x,y,z);
            if(ext =="Q"){
                break;
            }
        }
        }
    }
}

I tried to write ext inside while loop but I can't

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Learner
  • 3
  • 3
  • 1
    I guess it is because, Console.ReadLine() is nullable. Here is a link that explains it : https://stackoverflow.com/questions/38370565/resharper-says-that-console-readline-returns-null-value. Also in future, give the exact line where you get a warning, it greatly helps to identify the problem. – Siegfried.V Dec 11 '22 at 09:08
  • Does this answer your question? [ReSharper says that Console.ReadLine() returns null value](https://stackoverflow.com/questions/38370565/resharper-says-that-console-readline-returns-null-value) – Siegfried.V Dec 11 '22 at 09:09

5 Answers5

1

The warning is to tell you that the right side expression could return a null value and in your case, the left side of the variable is not nullable.

Refer the declaration of Console.ReadLine, it returns string?.

so you can declare it like string? ext = Console.ReadLine(); to get rid of this warning.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
1

You can define a variable with a null default like

int? num = Convert.ToInt32(Console.ReadLine());
Paul Albers
  • 121
  • 1
  • 8
  • 1
    [Convert.ToInt32](https://learn.microsoft.com/en-us/dotnet/api/system.convert.toint32) will always return an int, not a nullable int. – Klaus Gütter Dec 11 '22 at 11:27
1

The warning is related to assigning a nullable type to nun-nullable type. A simple alternative is to use int.tryparse() method like this:

int num;
int.TryParse(Console.ReadLine(),out num);
RezA
  • 103
  • 1
  • 8
0

Try replacing Convert.ToInt32 with int.Parse

v1cc--
  • 1
  • 1
    [Convert.ToInt32](https://learn.microsoft.com/en-us/dotnet/api/system.convert.toint32) is not the culprit here, it can handle a null input gracefully (returning 0). – Klaus Gütter Dec 11 '22 at 11:28
0

this is what I did to fix your code a little bit I put the condition inside the while loop and also you forgot to update ext after every iterration additionally I changed the method of getting the input of int to int.Parse instead of your Convert.ToInt32. try this and I am sure it will work as intended.

Console.WriteLine("Choose option: \n1. +\n2. -\n3. *\n4. /");
        int num = Convert.ToInt32(Console.ReadLine());
        if (num == 1)
        {
            string ext = Console.ReadLine();
            while (!ext.Equals("Q"))
            {
                Console.WriteLine("Enter numbers or tap Q to exit:");
                int x = int.Parse(Console.ReadLine());
                int y = int.Parse(Console.ReadLine());
                int z = x + y;
                Console.WriteLine("{0}+{1}={2}", x, y, z);
                ext = Console.ReadLine();
            }

        }