保存对 int 的引用
这是我想要做的事情的一个简化版本
static void Main(string[] args)
{
int test = 0;
int test2 = 0;
Test A = new Test(ref test);
Test B = new Test(ref test);
Test C = new Test(ref test2);
A.write(); //Writes 1 should write 1
B.write(); //Writes 1 should write 2
C.write(); //Writes 1 should write 1
Console.ReadLine();
}
class Test
{
int _a;
public Test(ref int a)
{
_a = a; //I loose the reference here
}
public void write()
{
var b = System.Threading.Interlocked.Increment(ref _a);
Console.WriteLine(b);
}
}
在我的真实代码中,我有一个 int ,它将由许多线程递增,但是在线程 a 调用的地方,向它传递指向 int 的参数并不容易(在实际代码中,这发生在 IEnumerator
内)。所以一个要求是必须在构造函数中进行引用。另外,并非所有线程都会指向相同的单基 int,因此我也不能使用全局静态 int。我知道我可以将 int 装入类中并传递该类,但我想知道这是否是执行此类操作的正确方法?
我认为可能是正确的方法:
static void Main(string[] args)
{
Holder holder = new Holder(0);
Holder holder2 = new Holder(0);
Test A = new Test(holder);
Test B = new Test(holder);
Test C = new Test(holder2);
A.write(); //Writes 1 should write 1
B.write(); //Writes 2 should write 2
C.write(); //Writes 1 should write 1
Console.ReadLine();
}
class Holder
{
public Holder(int i)
{
num = i;
}
public int num;
}
class Test
{
Holder _holder;
public Test(Holder holder)
{
_holder = holder;
}
public void write()
{
var b = System.Threading.Interlocked.Increment(ref _holder.num);
Console.WriteLine(b);
}
}
还有比这更好的方法吗?
Here is a much simplified version of what I am trying to do
static void Main(string[] args)
{
int test = 0;
int test2 = 0;
Test A = new Test(ref test);
Test B = new Test(ref test);
Test C = new Test(ref test2);
A.write(); //Writes 1 should write 1
B.write(); //Writes 1 should write 2
C.write(); //Writes 1 should write 1
Console.ReadLine();
}
class Test
{
int _a;
public Test(ref int a)
{
_a = a; //I loose the reference here
}
public void write()
{
var b = System.Threading.Interlocked.Increment(ref _a);
Console.WriteLine(b);
}
}
In my real code I have a int that will be incremented by many threads however where the threads a called it will not be easy to pass it the parameter that points it at the int(In the real code this is happening inside a IEnumerator
). So a requirement is the reference must be made in the constructor. Also not all threads will be pointing at the same single base int so I can not use a global static int either. I know I can just box the int inside a class and pass the class around but I wanted to know if that is the correct way of doing something like this?
What I think could be the correct way:
static void Main(string[] args)
{
Holder holder = new Holder(0);
Holder holder2 = new Holder(0);
Test A = new Test(holder);
Test B = new Test(holder);
Test C = new Test(holder2);
A.write(); //Writes 1 should write 1
B.write(); //Writes 2 should write 2
C.write(); //Writes 1 should write 1
Console.ReadLine();
}
class Holder
{
public Holder(int i)
{
num = i;
}
public int num;
}
class Test
{
Holder _holder;
public Test(Holder holder)
{
_holder = holder;
}
public void write()
{
var b = System.Threading.Interlocked.Increment(ref _holder.num);
Console.WriteLine(b);
}
}
Is there a better way than this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上,答案是肯定的,你需要上课。
不存在可以存储为字段的“引用 int”的概念。在 C# 中,它仅限于参数。
虽然存在一种
不安全
方式(指向 int、int*
),但在这种情况下处理 GC 的复杂性使其不切实际且效率低下。所以你的第二个例子看起来不错。
Basically, the answer is Yes, you need a class.
There is no concept of 'reference to int' that you can store as a field. In C# it is limited to parameters.
And while there is an
unsafe
way (pointer to int,int*
) the complexities of dealing with the GC in that scenario make it impractical and inefficient.So your second example looks OK.
您无法存储对变量的引用,正是因为有人可以做您正在做的事情:获取对局部变量的引用,然后在回收局部变量的存储后使用该引用。
将变量放入类的字段的方法很好。做同样事情的另一种方法是让 getter 和 setter 委托给变量。如果委托在外部局部变量上关闭,则该外部局部变量将被提升到一个字段,以便其生命周期比委托的生命周期长。
You cannot store a reference to a variable, for precisely the reason that someone could do what you are doing: take a reference to a local variable, and then use that reference after the local variable's storage is reclaimed.
Your approach of making the variable into a field of a class is fine. An alternative way of doing the same thing is to make getter and setter delegates to the variable. If the delegates are closed over an outer local variable, that outer local will be hoisted to a field so that its lifetime is longer than that of the delegates.
无法将引用存储为字段。
您需要在班级中保存 int 。
It is not possible to store a reference as a field.
You need to hold the int in a class.