C# if If Statement is True Declare Variable

Hi everyone, how do declare a variable inside of an if statement? I want to declare a variable when a specific if statement is true. But I’m not sure how.

You cannot actually do that, but here’s an alternative option. Suppose that you have a class Foo,

Foo instance = null;
if (x == 1)
{
  instance = new Foo(); 
}

Does that solve your problem? If not, let me know more specifically what the problem is.