Click here to Skip to main content
15,881,938 members
Home / Discussions / C#
   

C#

 
AnswerRe: Anonymous methods problem Pin
Christian Graus30-Jun-09 10:29
protectorChristian Graus30-Jun-09 10:29 
GeneralRe: Anonymous methods problem Pin
Narvius30-Jun-09 10:37
Narvius30-Jun-09 10:37 
GeneralRe: Anonymous methods problem Pin
Christian Graus30-Jun-09 13:29
protectorChristian Graus30-Jun-09 13:29 
GeneralRe: Anonymous methods problem Pin
Narvius30-Jun-09 13:48
Narvius30-Jun-09 13:48 
GeneralRe: Anonymous methods problem Pin
dojohansen1-Jul-09 0:27
dojohansen1-Jul-09 0:27 
GeneralRe: Anonymous methods problem Pin
Narvius1-Jul-09 4:20
Narvius1-Jul-09 4:20 
GeneralRe: Anonymous methods problem Pin
dojohansen1-Jul-09 4:50
dojohansen1-Jul-09 4:50 
GeneralRe: Anonymous methods problem [modified] Pin
Narvius1-Jul-09 6:32
Narvius1-Jul-09 6:32 
I've checked it.

It always has the scope of the place where it was created.

public void init()
{
  int i = 30;
  foo(delegate()
  {
    i = 10;
  });
  Console.WriteLine("i = " + i.ToString());
  Console.ReadKey();
}

public void foo(EventHandler awesome)
{
  awesome();
}


This would return "i = 10". This is somehow creepy, because it's a variable local in another method...
The same happens if foo is in another class.

Additionally, if the creator object doesn't exist anymore... nothing happens:

using System;
using System.Collections.Generic;
using System.Text;

namespace test2
{
    class Program
    {
        static void Main(string[] args)
        {
            Thing1 thing1 = new Thing1();
            Thing2 thing2 = thing1.Get2();
            thing1 = null;
            GC.Collect(); // Force Garbage Collection to be sure that thing1 doesn't exist anymore.
            thing2.awesome(null, null);
            return;
        }
    }

    public class Thing1
    {
        public Thing2 Get2()
        {
            Thing2 thing2 = new Thing2();
            thing2.awesome = delegate(object sender, EventArgs args)
            {
                thing2 = null;
            };
            return thing2;
        }
    }

    public class Thing2
    {
        public EventHandler awesome;
    }
}


Cleary, thing1 created the anonymous delegate.
I've named the Thing2 variable the same everywhere, to be sure.
Main.thing2 didn't turn into null, therefore it affected the non-existing thing1, ie. nothing happened. Also, no crash nor anything of that sort. It obviously has the scope of Thing1.Get2(), again the creator scope.

[Edit]
I've just noticed. The Garbage Collection doesn't take thing1!
The anonymous delegate points to it after all...

modified on Wednesday, July 1, 2009 2:20 PM

GeneralRe: Anonymous methods problem Pin
dojohansen1-Jul-09 21:05
dojohansen1-Jul-09 21:05 
GeneralRe: Anonymous methods problem Pin
Narvius2-Jul-09 0:06
Narvius2-Jul-09 0:06 
QuestionCopy project files to a new project Pin
Member 354381030-Jun-09 9:17
Member 354381030-Jun-09 9:17 
AnswerRe: Copy project files to a new project Pin
Narvius30-Jun-09 10:18
Narvius30-Jun-09 10:18 
GeneralRe: Copy project files to a new project Pin
dojohansen1-Jul-09 0:36
dojohansen1-Jul-09 0:36 
QuestionUninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 9:02
choo_chu30-Jun-09 9:02 
AnswerRe: Uninstalling (setup and d eployment project) - newbie question Pin
EliottA30-Jun-09 9:19
EliottA30-Jun-09 9:19 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 9:52
choo_chu30-Jun-09 9:52 
AnswerRe: Uninstalling (setup and d eployment project) - newbie question Pin
Jimmanuel30-Jun-09 10:08
Jimmanuel30-Jun-09 10:08 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 10:45
choo_chu30-Jun-09 10:45 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
choo_chu30-Jun-09 11:22
choo_chu30-Jun-09 11:22 
GeneralRe: Uninstalling (setup and d eployment project) - newbie question Pin
Jimmanuel30-Jun-09 11:27
Jimmanuel30-Jun-09 11:27 
Questionoverriding System.Diagnostics..::.DebuggerStepThroughAttribute Pin
Dan Neely30-Jun-09 8:45
Dan Neely30-Jun-09 8:45 
AnswerRe: overriding System.Diagnostics..::.DebuggerStepThroughAttribute Pin
Rob Smiley30-Jun-09 11:43
Rob Smiley30-Jun-09 11:43 
GeneralRe: overriding System.Diagnostics..::.DebuggerStepThroughAttribute Pin
Dan Neely1-Jul-09 2:13
Dan Neely1-Jul-09 2:13 
Questionserial port communication between computer and another hardware device via RS232 in C# for windows Application Pin
Arpita Patel30-Jun-09 6:52
Arpita Patel30-Jun-09 6:52 
AnswerRe: serial port communication between computer and another hardware device via RS232 in C# for windows Application Pin
monstale30-Jun-09 7:08
monstale30-Jun-09 7:08 

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.