C# - Why can't I pass a class declared in a using statement as a reference type?

后端 未结 1 1787
夕颜
夕颜 2021-02-12 21:22

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

public class DisposableInt : IDisposable
{
    private int? _Value;

    public int? My         


        
相关标签:
1条回答
  • 2021-02-12 22:02

    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.

    0 讨论(0)
提交回复
热议问题