Reputation: 11
I got simple problem, but couldn't find solution:
Is it possible to check if given variable has been assigned?
int i;
// stuff happens
if (someTest(i));
i = 0;
Console.Write("now i is assigned for sure")
Upvotes: 0
Views: 716
Reputation: 63105
In case of you need to know given field set or not, you can have Property to control that field. in the setter you can control it, for example in below example isSet Boolean flag is updated when the value set. if you need to reset the flag based on another value you can add another condition in the setter.
private int i;
private bool isSet;
public int IProp
{
get { return i;}
set { isSet =true; i=value; }
}
// test
Console.WriteLine("Is Set:" + isSet);
IProp = 0;
Console.WriteLine("Is Set:" + isSet);
//results
//Is Set:False
//Is Set:True
Upvotes: 0
Reputation: 416179
For value types, the variable is always assigned. There is a value there of some kind. Even so, if you try to read the variable before it is assigned the compiler will tell you and show an error: your code will not compile.
Upvotes: 4