By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,202 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,202 IT Pros & Developers. It's quick & easy.

CSharp compile's bug?

P: n/a
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?
May 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
string st"str"; was not reachable

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

"www.s1985.com" <ww*********@discussions.microsoft.com> wrote in message news:93**********************************@microsof t.com...
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?

May 12 '06 #2

P: n/a


"MSDN" wrote:
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.
May 12 '06 #3

P: n/a
www.s1985.com <ww*********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.