C#中:保持ref参数在类的构造函数 [英] c#: keep ref parameter from constructor in class

查看:133
本文介绍了C#中:保持ref参数在类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我希望能够有一个类的实例中一个变量的引用,但我想引用成为一个类变量,所以我不需要周围发送它的类作为参数的内部



代码:

  INT NUM = 0; 
MyClass的(NUM);
打印张数; //输出为0,但我想它是10 :)
类myClass的
{
私人诠释classNumber;
MyClass的(参考INT数)
{
打印数量; //输出为0

//这样ID是对refenrence
classNumber参考=号;

DoSomething的();
}
公共无效DoSomething的()
{
ClassNumber = 10;
}
}



为什么我问这个是因为我用的WinForms工作,具有主窗体发送一个类的实例,以新的形式,应该编辑类,并发送回..现在我使用Form.ShowDialog(),以避免用户使用的主要形式,而编辑新的形式,之后抓住来自新形式

  editForm edtfrm =新editForm(REF instanceOfClass)中的数据; 
edtfrm.showDialog();
//抢实例重新
instanceOfClass = edtfrm.editedClass;



我怎样才能解决这个问题?我不喜欢这种解决方案


解决方案

我想能有一个参考一个类的实例中的变量,但我想引用成为一个类变量,所以我不需要周围发送它的类里面的参数




您将有失望的生活呢。 CLR类型系统明确禁止以变量的类成员引用的存储。在CLR允许引用变量是




  • 传递给方法的相应正式参数或本
  • $ B $参数b
  • 存储为当地人

  • 返回方法的返回值



但确实的的许可证存储阵列中,字段等。基本上,凡是去堆不能守住一个引用



C#暴露出第一特征:裁判给变量作为方法参数。它不公开的另外两个特征(虽然我已经写的C#这确实一个实验性的版本,它的工作原理相当不错。)



需要注意的是C#不允许使用在裁判将要求裁判的堆存储环境 - 就像一个ref参数是一个封闭在一个lambda外变量,例如。有一些罕见的案件中,编译器允许什么样子裁判的长期存储,并使用拷入拷出语义模仿裁判,但可能是最好的甚至没有去那里。



为什么CLR这个限制?想想它的正确方法是,有两种存储器:长期和短期,通常被称为堆和栈。但数据结构的形状是无关紧要;什么是相关的是寿命的长度。一个变量有一个存储位置;这是一个变量是什么。如果你能保持一个裁判在长期储存短期储存分配一个变量,然后长期存放养了裁判的东西是寿命较短,因此可能会崩溃和死亡时,它访问变量后其死亡。



显然有许多方法来解决这个问题。例如,CLR团队可以选择,使其违法采取裁判短期储存,并允许在长期储存裁判的存储。但是,这则意味着你不能把裁判局部变量或参数,你想放的短期存储,因为他们的生命是如此短暂。



在CLR团队实际上选择的办法是禁止任何裁判的长期储存。像任何设计决策,这是对竞争的目标很多权衡的结果。


Basically i would like to be able to have a reference to a variable inside a instance of a class, but i'd like the reference to become a class variable so I dont need to send it around inside of the class as parameter

code:

int num = 0;
myClass(num);
print num; // output is 0 but i'd like it to be 10 :)
class myClass
{
    private int classNumber;
    myClass(ref int number)
    {
        print number; //output is 0

        // id like this to be a reference to the refenrence
        classNumber = number;

        DoSomething();
    }
    public void DoSomething()
    {
        ClassNumber = 10;
    }
}

why i'm asking this is because i'm working with winforms and having a main form sending an instance of a class to a new form that should edit the class and send it back.. right now i use Form.ShowDialog() to avoid the user to use the main form while editing in the new form and after that grabbing the data from the new forms

editForm edtfrm = new editForm(ref instanceOfClass);
edtfrm.showDialog();
//grab the instance back
instanceOfClass = edtfrm.editedClass;

How can i solve this? i don't like this solution

解决方案

i would like to be able to have a reference to a variable inside a instance of a class, but i'd like the reference to become a class variable so I dont need to send it around inside of the class as parameter

You will have to live with disappointment then. The CLR type system explicitly forbids storage of references to variables as members of classes. The CLR permits references to variables to be

  • passed to methods as arguments corresponding to formal parameters or 'this'
  • stored as locals
  • returned as method return values

but does not permit storage in arrays, fields, and so on. Basically, anything that goes "on the heap" can't hold onto a ref.

C# exposes the first feature: refs to variables as method parameters. It does not expose the other two features (though I have written an experimental version of C# which does, and it works quite nicely.)

Note that C# does not allow you to use refs in contexts which would require heap storage of the ref -- like a ref parameter being a closed-over outer variable of a lambda, for example. There are a few rare cases in which the compiler does allow what looks like long-term storage of the ref, and uses copy-in-copy-out semantics to emulate the ref, but probably best to not even go there.

Why does the CLR have this restriction? The right way to think about it is that there are two kinds of storage: long term and short term, usually called "heap" and "stack". But the shape of the data structure is irrelevant; what is relevant is the length of the lifetime. A variable has a storage location; that's what a variable is. If you could keep a ref to a variable allocated from the short-term storage in a long-term storage then the long-term storage keeps a ref to something that is of shorter lifetime, and therefore might crash and die when it accesses the variable after its death.

Obviously there are many ways to solve this problem. For example, the CLR team could have chosen to make it illegal to take a ref to short-term storage, and allow storage of refs in long-term storage. But that then means that you can't take refs to local variables or parameters, which you would like to put in short-term storage because their lives are so short.

The way the CLR team actually chose was to disallow long-term storage of any ref. Like any design decision, it was the result of many tradeoffs against competing goals.

这篇关于C#中:保持ref参数在类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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