C# - 为什么我不能传递一个使用语句引用类型声明的类? [英] C# - Why can't I pass a class declared in a using statement as a reference type?

查看:124
本文介绍了C# - 为什么我不能传递一个使用语句引用类型声明的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下的一次性类和示例代码来运行它:

 公共类DisposableInt:IDisposable的
{
私人诠释? _值;

公众诠释?敏
{
{返回_Value; }
集合{_Value =价值; }
}

公共DisposableInt(INT与InitialValue)
{
_Value =与InitialValue;
}

公共无效的Dispose()
{
_Value = NULL;
}
}

公共类TestAnInt
{
私人无效AddOne(参考DisposableInt INTVAL)
{
IntVal.MyInt ++ ;
}

公共无效的TestIt()
{
DisposableInt的INT;使用
(=的INT新DisposableInt(1))
{
Console.WriteLine(的String.Format(INT值:{0},TheInt.MyInt));
AddOne(REF的INT);
Console.WriteLine(的String.Format(智力值+ 1:{0},TheInt.MyInt));
}
}
}

调用新TestAnInt()的TestIt()运行良好。但是,如果我更改的TestIt() DisposableInt 是using语句,像这样的内部声明:

 公共无效的TestIt()
{
// DisposableInt的INT;使用(DisposableInt =的INT新DisposableInt(1))
{
Console.WriteLine(的String.Format(INT值:{0},TheInt.MyInt))
;
AddOne(REF的INT);
Console.WriteLine(的String.Format(智力值+ 1:{0},TheInt.MyInt));
}
}



我得到以下编译器错误:



 不能通过'的INT'作为ref或out参数,因为它是一个'使用变量

这是为什么?所不同的是,除了类超出作用域和垃圾收集一旦使用语句完成?



(和是的,我知道我的一次性类的例子是相当愚蠢的,我只是想简单起见)


解决方案

这是为什么?




变量声明的在使用语句只读; 退出 REF 参数都没有。所以,你的可以的做到这一点:

  DisposableInt =的INT新DisposableInt(1); 
使用(的INT)
{
AddOne(REF的INT);
}



...但根本你没有的使用事实上,它是一个 REF 参数反正...



这有可能是你误解了 REF 的真正意义。这将是读通过,以确保你真正了解我的参数文章是一个好主意。


Suppose I have the following disposable class and example code to run it:

public class DisposableInt : IDisposable
{
    private int? _Value;

    public int? MyInt
    {
        get { return _Value; }
        set { _Value = value; }
    }

    public DisposableInt(int InitialValue)
    {
        _Value = InitialValue;
    }

    public void Dispose()
    {
        _Value = null;
    }
}

public class TestAnInt
{
    private void AddOne(ref DisposableInt IntVal)
    {
        IntVal.MyInt++;
    }

    public void TestIt()
    {
        DisposableInt TheInt;
        using (TheInt = new DisposableInt(1))
        {
            Console.WriteLine(String.Format("Int Value: {0}", TheInt.MyInt));
            AddOne(ref TheInt);
            Console.WriteLine(String.Format("Int Value + 1: {0}", TheInt.MyInt));
        }
    }
}

Calling new TestAnInt().TestIt() runs just fine. However, if I change TestIt() so that DisposableInt is declared inside of the using statement like so:

    public void TestIt()
    {
        // DisposableInt TheInt;
        using (DisposableInt TheInt = new DisposableInt(1))
        {
            Console.WriteLine(String.Format("Int Value: {0}", TheInt.MyInt));
            AddOne(ref TheInt);
            Console.WriteLine(String.Format("Int Value + 1: {0}", TheInt.MyInt));
        }
    }

I get the following compiler error:

Cannot pass 'TheInt' as a ref or out argument because it is a 'using variable'

Why is this? What is different, other than the class goes out of scope and to the garbage collector once the using statement is finished?

(and yeah, I know my disposable class example is quite silly, I'm just trying to keep matters simple)

解决方案

Why is this?

Variables declared in using statements are read-only; out and ref parameters aren't. So you can do this:

DisposableInt theInt = new DisposableInt(1);
using (theInt)
{
    AddOne(ref theInt);
}

... but fundamentally you're not using the fact that it's a ref parameter anyway...

It's possible that you've misunderstood what ref really means. It would be a good idea to read my article on parameter passing to make sure you really understand.

这篇关于C# - 为什么我不能传递一个使用语句引用类型声明的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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