Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
参考var c#_C# - Fatal编程技术网

参考var c#

参考var c#,c#,C#,我想知道是否有一种方法可以使用像'ref'这样的var引用,但不能在方法中使用。 例如: using System; using System.Collections; using System.Collections.Generic; public class Class3 { struct myStruct { public bool structBool; public int structInt; public myStr

我想知道是否有一种方法可以使用像'ref'这样的var引用,但不能在方法中使用。 例如:

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)
            }
        }
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
公共班级3
{
结构myStruct
{
公共布尔结构布尔;
公共int-structin;
公共myStruct(bool _structBool,int _structin)
{
structBool=_structBool;
structInt=_structInt;
}
}
myStruct当前任务;
int值1、值2;
bool mybool,isProcessing;
队列myTask=新队列();
void main()
{
//这两行不工作,由于“参考”,但我正在寻找这样的工作
if(value1>value2)myTask.Enqueue(newmystruct(mybool,ref value1));
if(value2>value1)myTask.Enqueue(newmystruct(mybool,ref value2));
MyFunction();
}
void MyFunction()
{
如果(myTask.Count>0)
{
如果(!isProcessing)
{
currentTask=myTask.Dequeue();
isProcessing=true;
}
其他的
{
currentTask.structInt++;//这里我需要捕获我的变量(value1或value2)
}
}
}
}

我试图将值放入数组中,但我认为这是一种糟糕的方法。我尝试了很多其他东西,但都没有正常工作。

通常,当你想给一个构造函数(甚至是一个方法)赋予多个值时,你可以将它们作为类的一部分给予:

public class Args
{
     public int Value { get; set; }
}
现在你可以这样做了:

Args args1 = new Args { Value = 10 };
Args args2 = new Args { Value = 34 };

// Obviously, your structs or classes should accept Args class as input parameter
var struct1 = new MyStruct(true, args1);
var struct2 = new MyStruct(false, args2);

现在,结构构造函数调用者可以修改
Args.Value1
和/或
Args.Value2

您可以更改构造函数以通过引用传递这些参数,如下所示:

public myStruct(bool\u structBool,ref int\u structin)

问题是调用这一行

currentTask.structInt++


仍然不会更改原始变量(
value1
value2
)。检查答案中的答案:

我没有抓住你问题的重点。
myStruct()
的构造函数不接受
ref
param,也不做任何需要将其设置为
ref
param的事情(比如分配给它)。为什么要将其作为
ref
传递?能否详细说明您的问题,而不是技术难题?也许有更好的办法?这是你想要的吗<代码>整数值1=2;var s=新的myStruct(mybool,参考值1);s、 structin=3;控制台写入线(值1);/*输出3*/
——也就是说,您是否在寻找C会调用指向int的指针的内容?我不明白您在问什么。你的代码中没有
var
。我检查了一下,但不明白这个线程如何解决我的问题。嗨。建议的解决方案与fidemraizer建议的@matí相同。其思想是将一个整数包装到另一个类中,以便
myStruct
MyFunction
都可以引用同一个实例。所以,如果你看一下Matias的代码:你可以只更改`int value1,value2;`to
Args args1=新Args();Args args2=新Args()
公共int-structin
公共Args structArgs
(同时更改构造函数),然后按如下方式递增:
currentTask.structArgs.Value++