Why I always need to assign a value to string variable, before actually using it to compare.
For ex:
Some input – obj
string temp;
if (obj== null)
{
temp = "OK";
}
string final = temp;
I get compile time error – something like – cant use unassigned variable ‘temp’. But string variable has default value as ‘null’, which I want to use. So why this is not allowed?
The default is not null (or anything else) for a local variable. It’s just unassigned.
You are probably thinking about a string field (a variable at the class level). That would be
null:But inside a method, just initialize with the value you need: