范围和如何使用缩小它VB.Net [英] Scope and how to narrow it using VB.Net

查看:127
本文介绍了范围和如何使用缩小它VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想缩小在C#中变量的范围内,我可以介绍更多的括号 - 例如:

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; }

    }
}

在IDE中,我可以不再引用ý由新括号引入的范围之外。我本来以为这将意味着变量y将可用于垃圾收集。

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)

有没有类似这样以任何方式使用VB.net时缩小范围?这是否有当在内部范围内定义的变量可以被垃圾收集任何影响?

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?

推荐答案

有趣的是,developerFusion C#-vb .NET代码转换器把

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

为限制范围的一种方式。我很惊讶,它困扰记住 paintballbob的轴承回答

as a way of limiting the scope. I'm surprised it bothers bearing in mind paintballbob's answer

这篇关于范围和如何使用缩小它VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆