两个在C#中声明的相同变量

Thi*_*ude -3 c# out

我在我的程序中找到了一个在线代码,但令我惊讶的是我发现有一个变量/函数声明了两次......

现在,如果我要向DLL发送任何值,我将发送信息到哪两个?一个用完,而第二个没用... 参见代码:

[DllImport("msman.dll", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=false)]
        public static extern bool receive(int ID, double[] Bid, double[] Ask);

        public bool receive(int ID, out double first, out double second)
        {
            bool flag;
            double[] Array1st = new double[1];
            double[] Array2nd = new double[1];
            if (Form1.receive(ID, Array1st, Array2nd))
            {
                first = Array2nd[0];
                second = Array1st[0];
                flag = true;
            }
            else
            {
                second = 0;
                first = 0;
                flag = false;
            }
            return flag;
        }
Run Code Online (Sandbox Code Playgroud)

而且,为什么可以声明两个变量..

Col*_*inD 6

我的C#不是很好,但这看起来像方法重载的标准情况.

请注意每种方法的签名

A: public static extern bool receive(int ID, double[] Bid, double[] Ask);
B: public bool receive(int ID, out double first, out double second)
Run Code Online (Sandbox Code Playgroud)

A采用下列参数:int, double[], double[] 当B需要int, double, double.注意类型的差异.因此,当你调用它时,编译器会说"哦,你想用一个int和两个双数组来调用receive.得到它,你走吧!" 服务A

呼叫如何工作的示例:

int x = 1; double y = 1.0; double z = 2.0;
receive(x, y, z); // <-- this calls the second method (B).

int x = 1; double[] y = new double[1]; double[]z = new double[1];
y[0] = 1.0;
z[0] = 1.0;
receive(x, y, z); // <-- this calls the first method (A)
Run Code Online (Sandbox Code Playgroud)