Question Getting CS8600 In Console Application

The

New member
Joined
Sep 24, 2022
Messages
1
Programming Experience
Beginner
So, I'm writing a Console RPG game thing, to become more proficient in C#, and at one line, I get CS8600, and I'm not sure why. it is a command system thing, and for some reason it closes the command window as if there is no more code after (There is, I will put it below). Please help me! (And yes, I've already tried putting a '!' in Console.ReadLine(); it closes anyway.)
Fight():
 public static int Fight()
    {
        
        Random rng = new Random();
        Random dmg = new Random();
        string[] monsters = {"skeleton", "goblin", "zombie"};
        int[] monstersHealth = {3, 2, 5};
        int monsterPicked = rng.Next(0, 3);
        int monsterPickedHealth = monstersHealth[monsterPicked];
        string monsterName = monsters[monsterPicked];
        Console.Write("You adventure down a cave, and find a " + monsterName + " blocking your path!\n");
        Console.Write("What would you like to do?\n Attack, Guard, Run\n");
        string fightCommand = Console.ReadLine();
        Console.Write("You wrote " + fightCommand + "!");
        if (fightCommand == "attack" || fightCommand == "Attack")
        {
            int damage = dmg.Next(1, 4);
            Console.Write("You use your trusty wooden sword to attack the " + monsterName + ".\nYou do " + damage + " damage!\n");
            int result1 = monsterPickedHealth - damage;
            Console.Write("The " + monsterName + " has " + result1 + " health left!");
            
            
        }
        return 0;
    }
 
For the record, when you use the code editor on this site, you can specify one or more line numbers to highlight. You could have done that to highlight the line that generates the error. Now I have to ask you which line this happens and you will have to wait longer to get help.

Also, you've given us an error code like we should have them memorised. We would have to search the web for that code to see what it actually means when you could have just given us the error message. The more relevant information you provide, the more chance you'll get the help you want and the sooner you're likely to get it.
 
Looking at your code, one thing that immediately jumps out is that you are creating two Random objects in a method. That's doubly wrong. Triply wrong, in fact. It's unlikely to be the cause of your issue but it's very wrong

Firstly, consider the logic of the situation. If you're playing a dice game, does each player have their own dice or do all players use the same dice? Would each player get more random results by using different dice to the other players? Random is random so two numbers generated by two random number generators aren't going to be more random than two numbers generated by the same random number generator. Using two Random objects serves no useful purpose, but it actually causes problems.

The Random classes actually generates a pseudo-random sequence of numbers based on a seed value. The default seed is based on the time and that, along with the algorithm used to generate the sequence, means that the results are random enough for most purposes. In your code though, you create two instances in a very short time, so there's every chance that they will use the same seed value and thus generate the same sequence of numbers. Not only is using two Random objects not more random, it's less.

What you should be doing is creating a single Random object for the application. If that method is called multiple times or you need random numbers elsewhere too, it should be created outside the method. You then use that one and only instance every time and every place you want a random number.
 
I just pasted your code into VS and I see that CS8600 is a warning, not an error, and the message is:
Converting null literal or possible null value to non-nullable type.
on this line:
C#:
string fightCommand = Console.ReadLine();
This means that you must be targeting .NET 6 and, if you read the documentation for that method for .NET 6 then you'll see that the return type is string? and you're assigning it to a string variable, i.e. a nullable to a non-nullable. To get rid of the warning, you can either declare your variable nullable:
C#:
string? fightCommand = Console.ReadLine();
or use var and let the type be inferred as string?:
C#:
var fightCommand = Console.ReadLine();
or else use the null forgiving operator:
C#:
string fightCommand = Console.ReadLine()!;
That will be fine in the context you're using this code, i.e. in a console window. I've debugged your original code and each of those variations and they all work. I don't know how you're testing but, for me, stepping through the code in the debugger, it works exactly as expected. It's just that your code does nothing unless the user enters "attack" or "Attack". If they enter anything else, the method just returns.
 
I'm not sure why. it is a command system thing, and for some reason it closes the command window as if there is no more code after.
Assuming you are running the Debug build of your console app, it's likely one or more of these situations:
1) You are using on older version of Visual Studio which automatically closes the console window with the program ends;
2) You created a Windows Form project, but using the console window;
3) With a newer version of Visual Studio, you turned off the option that will keep the console window after the program ends.
 
And yes, I've already tried putting a '!' in Console.ReadLine(); it closes anyway.
The exclamation mark in newer versions of the language just tells the compiler that you are know what you are doing with regards to possible null returns. It's not like some kind of magic marker telling the compiler or the runtime that the line must be executed. In C#, lines will be executed unless the compiler warns you that the code unreachable in simple cases, or you write logic that make lines of code unreachable in more complex cases. As noted above, the body of your if statement will be unreachable if the user doesn't type in exactly "attack" or "Attack".
 
Last edited:
Back
Top Bottom