我不知道是否有一種方法來使用類似'ref'的var引用,但不是在方法中。 爲例:參考var c#
using System;
using System.Collections;
using System.Collections.Generic;
public class Class3
{
struct myStruct
{
public bool structBool;
public int structInt;
public myStruct(bool _structBool, int _structInt)
{
structBool = _structBool;
structInt = _structInt;
}
}
myStruct currentTask;
int value1,value2;
bool mybool, isProcessing;
Queue<myStruct> myTask = new Queue<myStruct>();
void main()
{
//these two lines don't work due to the "ref" but I'm looking for something work like this
if (value1 > value2) myTask.Enqueue(new myStruct(mybool,ref value1));
if (value2 > value1) myTask.Enqueue(new myStruct(mybool,ref value2));
MyFunction();
}
void MyFunction()
{
if (myTask.Count > 0)
{
if (!isProcessing)
{
currentTask = myTask.Dequeue();
isProcessing = true;
}
else
{
currentTask.structInt++; // here I need to catch my var (value1 or value2)
}
}
}
}
我試圖把值到一個數組,但我認爲這是一個不錯的方法。我嘗試了很多其他的東西,但沒有正常工作。
我錯過了您的問題的要點。 'myStruct()'的構造函數不需要'ref'參數,也不需要做任何需要使其成爲'ref'參數的東西(比如賦值給它)。你爲什麼想把它作爲'ref'來傳遞? –
您能否詳細說明您的問題而不是技術上的困難?也許有更好的方法? – rsqLVo
這是你想要的嗎? 'int value1 = 2; var s = new myStruct(mybool,ref value1); s.structInt = 3; Console.WriteLine(值1);/*輸出3 * /' - 也就是說,你在尋找C會調用一個指向int的指針嗎? –