Click here to Skip to main content
16,006,440 members
Home / Discussions / C#
   

C#

 
GeneralRe: stack issue Pin
Goncalo Oliveira14-Jan-08 6:27
Goncalo Oliveira14-Jan-08 6:27 
GeneralRe: stack issue Pin
Dave Kreskowiak14-Jan-08 12:01
mveDave Kreskowiak14-Jan-08 12:01 
GeneralRe: stack issue Pin
Goncalo Oliveira14-Jan-08 12:58
Goncalo Oliveira14-Jan-08 12:58 
GeneralRe: stack issue Pin
Skippums11-Jan-08 4:49
Skippums11-Jan-08 4:49 
GeneralRe: stack issue Pin
Goncalo Oliveira11-Jan-08 4:54
Goncalo Oliveira11-Jan-08 4:54 
GeneralRe: stack issue Pin
Skippums11-Jan-08 5:03
Skippums11-Jan-08 5:03 
GeneralRe: stack issue Pin
Goncalo Oliveira11-Jan-08 5:19
Goncalo Oliveira11-Jan-08 5:19 
GeneralRe: stack issue Pin
S. Senthil Kumar12-Jan-08 23:20
S. Senthil Kumar12-Jan-08 23:20 
One word : closures. A closure captures the value of a variable in its lexical scope.

Your event handlers are anonymous functions which capture the local variable text. Behind the scenes, the compiler generates a new class with the captured variable as a member and rewires the event handler so that it calls a method on the generated class. That class then uses the member variable's value to fill in for the captured variable.

The compiler updates the generated class instances with the value of captured variables as long as the captured variables are in scope. In your case, text was declared outside the scope of both if blocks, so the compiler dutifully updated the value of the variable when you changed it inside the second if block.

Just to make things clear, this is not done only for reference types (like string).
delegate void Func();

 static void Main()
 {
     Func[] f = new Func[10];
     for (int i = 0; i < 10; ++i)
     {
         f[i] = delegate { Console.WriteLine(i); };
     }

     foreach (Func func in f)
     {
         func();
     }
 }


This will print 10 all ten times.

Regards
Senthil [MVP - Visual C#]
_____________________________
My Blog | My Articles | My Flickr | WinMacro

GeneralRe: stack issue Pin
Goncalo Oliveira13-Jan-08 5:33
Goncalo Oliveira13-Jan-08 5:33 
QuestionHow i highLight Text Box Pin
wasimsharp11-Jan-08 0:01
wasimsharp11-Jan-08 0:01 
AnswerRe: How i highLight Text Box Pin
Stu Richardson11-Jan-08 0:43
Stu Richardson11-Jan-08 0:43 
AnswerRe: How i highLight Text Box Pin
Abhijit Jana11-Jan-08 4:08
professionalAbhijit Jana11-Jan-08 4:08 
GeneralWrite line of text to a list view Pin
CodingLover10-Jan-08 23:52
CodingLover10-Jan-08 23:52 
GeneralRe: Write line of text to a list view Pin
Thomas Stockwell11-Jan-08 1:30
professionalThomas Stockwell11-Jan-08 1:30 
GeneralRe: Write line of text to a list view Pin
CodingLover13-Jan-08 18:31
CodingLover13-Jan-08 18:31 
GeneralRe: Write line of text to a list view Pin
Thomas Stockwell14-Jan-08 0:19
professionalThomas Stockwell14-Jan-08 0:19 
GeneralRe: Write line of text to a list view Pin
CodingLover15-Jan-08 16:29
CodingLover15-Jan-08 16:29 
QuestionHow to create context menu for notify icon using windows service application in c#.net Pin
dharanighanta10-Jan-08 23:51
dharanighanta10-Jan-08 23:51 
AnswerRe: How to create context menu for notify icon using windows service application in c#.net Pin
Justin Perez11-Jan-08 2:36
Justin Perez11-Jan-08 2:36 
General[Message Deleted] Pin
michael_jhons10-Jan-08 23:37
michael_jhons10-Jan-08 23:37 
GeneralRe: please it's urgent ENABLE COOKIES Pin
Pete O'Hanlon10-Jan-08 23:56
mvePete O'Hanlon10-Jan-08 23:56 
GeneralRe: please it's urgent ENABLE COOKIES Pin
DavidNohejl11-Jan-08 1:14
DavidNohejl11-Jan-08 1:14 
QuestionReview table into 2 columns in report Pin
sgeorgije10-Jan-08 23:14
sgeorgije10-Jan-08 23:14 
Generallike sql statement in c# Pin
eyeseetee10-Jan-08 23:07
eyeseetee10-Jan-08 23:07 
GeneralRe: like sql statement in c# Pin
eyeseetee10-Jan-08 23:34
eyeseetee10-Jan-08 23:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.