T
Tony
Hello!
Here I have some text from a book I read. It says:
"An interesting point to note concerning anonymous methods is that they are
effectively local to the code
block that contains them, and they have access to local variables in this
scope. If you use such a variable
it becomes an outer variable. Outer variables are not disposed of when they
go out of scope like other
local variables are; instead, they live on until the anonymous method that
use them are destroyed.
This may be some time later than you expect and is definately something to
bed careful about."
I have one questions about the above text.
What does this mean in the text: "If you use such a variable
it becomes an outer variable. Outer variables are not disposed of when they
go out of scope like other
local variables are; instead, they live on until the anonymous method that
use them are destroyed.
This may be some time later than you expect and is definately something to
bed careful about."
In main below I have defined an anonymous method. Is it possible that you
can exemplify the
answer by using main below and the anonymous method.
static void Main(string[] args)
{
Connection myConnection1 = new Connection();
myConnection1.Name = "First connection.";
myConnection1.MessageArrived += delegate(object source, EventArgs e)
{
Console.WriteLine("Message arrived from: {0}",
((Connection)source).Name);
Console.WriteLine("Message Text: {0}",
((MessageArrivedEventArgs)e).Message);
};
myConnection1.Connect();
Console.ReadKey();
}
//Tony
Here I have some text from a book I read. It says:
"An interesting point to note concerning anonymous methods is that they are
effectively local to the code
block that contains them, and they have access to local variables in this
scope. If you use such a variable
it becomes an outer variable. Outer variables are not disposed of when they
go out of scope like other
local variables are; instead, they live on until the anonymous method that
use them are destroyed.
This may be some time later than you expect and is definately something to
bed careful about."
I have one questions about the above text.
What does this mean in the text: "If you use such a variable
it becomes an outer variable. Outer variables are not disposed of when they
go out of scope like other
local variables are; instead, they live on until the anonymous method that
use them are destroyed.
This may be some time later than you expect and is definately something to
bed careful about."
In main below I have defined an anonymous method. Is it possible that you
can exemplify the
answer by using main below and the anonymous method.
static void Main(string[] args)
{
Connection myConnection1 = new Connection();
myConnection1.Name = "First connection.";
myConnection1.MessageArrived += delegate(object source, EventArgs e)
{
Console.WriteLine("Message arrived from: {0}",
((Connection)source).Name);
Console.WriteLine("Message Text: {0}",
((MessageArrivedEventArgs)e).Message);
};
myConnection1.Connect();
Console.ReadKey();
}
//Tony