If I want to narrow scope of a variable in C#, I can introduce additional braces - i.e.:
class Program
{
static void Main(string[] args)
{
myClass x = new myClass();
x.MyProperty = 1000;
Console.WriteLine("x = " + x.MyProperty);
{
myClass y = new myClass();
y.MyProperty = 2000;
Console.WriteLine("y = " + y.MyProperty);
}
myClass y2 = new myClass();
y2.MyProperty = 3000;
Console.WriteLine("y2 = " + y2.MyProperty);
}
class myClass
{
public int MyProperty { get; set; }
}
}
In the ide, I can no longer reference y outside of the scope introduced by the new braces. I would have thought that this would mean that the variable y would be available for garbage collection.
(it is interesting to note that when viewing the compiled code using reflector it appears that there is no difference with or without the additional braces)
Is there any way similar to this to narrow scope when using VB.net? Does this have any impact on when variables defined in the inner scope may be garbage collected?
Interestingly, the developerFusion c#-vb.net code converter converts
{
myClass y = new myClass();
y.MyProperty = 2000;
Console.WriteLine("y = " + y.MyProperty);
}
to
If True Then
Dim y As New [myClass]()
y.MyProperty = 2000
Console.WriteLine("y = " & y.MyProperty)
End If
as a way of limiting the scope. I'm surprised it bothers bearing in mind paintballbob's answer
there doesn't seem to be a good way to create a new scope in vb, but you can make a loop that runs only once guaranteed and then declare your variable inside that loop.
MSDN had this to say about the lifetime of a variable:
Even if the scope of a variable is limited to a block, its lifetime is still that of the entire procedure. If you enter the block more than once during the procedure, each block variable retains its previous value. To avoid unexpected results in such a case, it is wise to initialize block variables at the beginning of the block.
src: http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx
it seems that the variables are only subject to garbage collection once the procedure has finished, but even then the garbage collector will not run unless the heap is getting crowded. Most likely for small apps nothing ever gets garbage collected until the app is closed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With