In Python you could do exactly that:
if error:
message = "there was an error";
else:
message = "no error";
print(message);
Or even:
message = "there was an error" if error else "no error"
This is possible because Python does not require you to define the variable: the first time you assign a value to a variable, it is created, next times it is modified.
However, this is not the case of C#; as you yourself have confirmed, there are various caveats that do not allow you to code as in Python. First of all, you need to define each variable you are going to use (and its type, but keep reading), and secondly, there is scope. In C# a variable created inside an if branch will not exist further the end of that branch.
And well, then there is var. This is a special shortcut to the type of the value at the right of the assignment operator ("="). It does not mean "I will tell you the type of the variable later". It means "the type of the variable is the same of the expression I'm assigning it to.". In other words, you can only use var when you're assigning a value to a variable at the same time you are defining the variable.
Hope this helps.
var message=string.Empty;?varis just a shortcut for the type, so you explicitely define it anyway. It´s clearly stated on MSDN: "The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement". In your case there *is no * expression on the right side.var message = "";that would define var as string in this case and could work.string message;is shorter