“Only assignment, call, increment, decrement, and new object expressions can be used as a statement”

StackOverflow https://stackoverflow.com/questions/12102082

  •  28-06-2021
  •  | 
  •  

Question

I am getting this error in my code for a c# console application

 case 5:
    Console.WriteLine("User selected to Quit, option " + response);
    Environment.Exit;
    break;

// Error Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Was it helpful?

Solution

Environment.Exit is a method - you need to add brackets to call it:

Environment.Exit(0);

Edit: sorry, I should have paid more attention. Added the argument. Thanks 0___0

OTHER TIPS

Since Enviroment.Exit is a method, so you need to do

Enviroment.Exit(0);

Here 0 is the intcode provided to OS.

You can read about it here

This is more generic answer "got error - what to do" as you already have answer to your particular question: Environment.Exit is not a call.

All C# compiler errors have error code associated with it. I.e. in your case you'll see something like this in output:Compile window:

CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

MSDN already provides explanations for a lot of common errors. Basic search for error code http://www.bing.com/search?q=CS0201 instantly gives you link to the article on MSDN - Compiler Error CS0201. In your particular case it may not have helped (as linked article is not covering this case) but consider such search as first step in future when you encounter unclear error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top