PC Review


Reply
Thread Tools Rate Thread

Absolute newbie question

 
 
Henning
Guest
Posts: n/a
 
      27th Sep 2011

I'm completely new to C#, although I have some background in TurboPascal
many years ago.

What I need is the explanation of why the first example gets and "use of
unassigned variable" when the variable is referenced outside the while
loop, while the second example works ok.

[example1]
static void Main(string[] args) {
bool ok = true; int checkvalue;
while (ok) {
checkvalue = 5;
Console.WriteLine("checkvalue in while loop {0} ", checkvalue);
ok = false; }
Console.WriteLine("checkvalue after the while loop {0} ", checkvalue);
Console.ReadKey();

[example2]
static void Main(string[] args) {
bool ok = true; int checkvalue=0;
while (ok) {
checkvalue = 5;
Console.WriteLine("checkvalue in while loop {0} ", checkvalue);
ok = false; }
Console.WriteLine("checkvalue after the while loop {0} ", checkvalue);
Console.ReadKey();

It's not that I don't got a clue or assumption, but I need the exact and
correct explanation/definiton

 
Reply With Quote
 
 
 
 
Henning
Guest
Posts: n/a
 
      27th Sep 2011
On 27-09-2011 18:21, Peter Duniho wrote:
> On 9/27/11 8:41 AM, Henning wrote:
>>
>> I'm completely new to C#, although I have some background in TurboPascal
>> many years ago.
>>
>> What I need is the explanation of why the first example gets and "use of
>> unassigned variable" when the variable is referenced outside the while
>> loop, while the second example works ok.

>
> The C# specification has precise rules regarding "definite assignment".
> Your first example does not meet the criteria required for the variable
> "checkvalue" to be considered "definitely assigned", hence the failure.
>
> The second example trivially meets the criteria, by assigning the
> variable at initialization.
>
> With respect to the first example, it's important to note that the C#
> specification does not require the compiler to do any analysis of loop
> conditions. So even though you as a human are aware that the loop is
> executed at least once, from the compiler's point of view all it knows
> is that there's a loop and that loop bodies can sometimes be skipped
> entirely. Since the only place the variable is definitely assigned is
> within the loop body, and the loop body can theoretically be skipped,
> the variable is considered to not be definitely assigned.
>
> You can download the v4 specification for C# here:
> http://www.microsoft.com/download/en...ang=en&id=7029
>
> Pete


Thanks for the concise and precise explanation.

 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      27th Sep 2011
"Henning" <(E-Mail Removed)> wrote in message
news:4e81eea4$0$292$(E-Mail Removed)...

> I'm completely new to C#, although I have some background in TurboPascal
> many years ago.


Hi, Henning!


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Absolute newbie question bm1968@gmx.net Microsoft VC .NET 2 19th Sep 2009 07:27 PM
Absolute question pgarcia Microsoft Excel Misc 5 1st Jan 2008 12:28 AM
Absolute newbie can't download a movie from digital camera Spamfree! Windows XP MovieMaker 1 3rd Dec 2007 05:54 PM
Absolute Reference Question for VBA =?Utf-8?B?SmFzb24=?= Microsoft Excel Misc 2 20th Mar 2004 03:21 AM
absolute newbie question - connecting to SQL server in ADO.net Chris Strug Microsoft ADO .NET 6 9th Mar 2004 07:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:23 PM.