CSharp compile's bug?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

bool b = true;
if(!b) goto end;
string str="str";
end:
Console.Write(str);

compiler throw error message 'unassign variable "str"'.
this error explain compiler was find variable define,
but don't execute assing sentence.
Why?Is't a bug?
 
string st"str"; was not reachable

bool b = true;
string str = string.Empty;
if (!b) goto end;
str = "str";
end:
Console.Write(str);
 
MSDN said:
string st"str"; was not reachable

bool b = true;
string str = string.Empty;
if (!b) goto end;
str = "str";
end:
Console.Write(str);
if not reachable so compiler should throw not find error.
but it throw unassign error.
so i think it's a bug.
 
www.s1985.com said:
bool b = true;
if(!b) goto end;
string str="str";
end:
Console.Write(str);

compiler throw error message 'unassign variable "str"'.
this error explain compiler was find variable define,
but don't execute assing sentence.
Why?Is't a bug?

No, it's not a bug. The C# compiler doesn't attempt to verify that the
"if" condition will never be satisfied unless the expression is a
constant, which in this case it isn't.

The variable str isn't definitely assigned as per the C# language
specification, hence the error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top