Can you talk about how you are going to use this? Based on this question and one of your previous questions, it looks like you are coming from a PHP background. In C # there is no concept of an undefined variable. At any point in the code, the given variable is declared or not, and you can determine whether it is declared or not by simply looking at the code. If it is not declared, the compiler will not allow you to use the variable at all (it does not exist). A variable can be declared, but not initialized; however, the compiler will not allow you to read the value of a variable unless you are sure that the variable has a value. For instance:
int foo; // Declared, but uninitialized if (bar == 42) foo = 3; // Initialize foo // At this point, foo may or may not be initialized. // Since we cannot be sure that it is initialized, // the next line will not compile. x = foo;
If you want to track whether a variable has been assigned a value (and you cannot use null to indicate that a value has not been assigned), you need to track this with a separate bool that starts with false and sets to true when you assign another a variable.
source share