在 C# 中声明的两个相同的变量
Two Same variable declared in C#
我在我的程序中使用了在线代码,但令我惊讶的是我发现有一个 variable/function 声明了两次...
现在,如果我要向 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;
}
还有,为什么可以声明两个变量..
我的 C# 不是很好,但这看起来像是 method overloading.
的标准情况
注意每个方法的签名
A: public static extern bool receive(int ID, double[] Bid, double[] Ask);
B: public bool receive(int ID, out double first, out double second)
A
采用以下参数:int, double[], double[]
而 B
需要 int, double, double
。注意类型的区别。所以当你调用它时,编译器会说 "oh, you want to call receive with an int and two double arrays. Got it, here you go!" 并提供 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)
方法可以重载,也就是说,只要参数不同,它们就可以有相同的名称(不允许只在return类型上重载)。
因此这是有效的:
void Foo(int x) { }
void Foo(char x) { }
或者,在您的情况下:
bool receive(int ID, double[] Bid, double[] Ask);
bool receive(int ID, out double first, out double second)
请注意,这是许多其他语言(包括 C++)的标准语言功能。
方法重载是面向对象的概念,其中方法可以具有相同的名称,只有参数(方法的签名)不同。
using System;
class Test
{
static void receive(int x, double y)
{
Console.WriteLine("receive(int x, double y)");
}
static void receive(double x, int y)
{
Console.WriteLine("receive(double x, int y)");
}
static void Main()
{
receive(5, 10.2);
}
}
看这里https://msdn.microsoft.com/en-us/library/aa691131(v=vs.71).aspx
我在我的程序中使用了在线代码,但令我惊讶的是我发现有一个 variable/function 声明了两次...
现在,如果我要向 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;
}
还有,为什么可以声明两个变量..
我的 C# 不是很好,但这看起来像是 method overloading.
的标准情况注意每个方法的签名
A: public static extern bool receive(int ID, double[] Bid, double[] Ask);
B: public bool receive(int ID, out double first, out double second)
A
采用以下参数:int, double[], double[]
而 B
需要 int, double, double
。注意类型的区别。所以当你调用它时,编译器会说 "oh, you want to call receive with an int and two double arrays. Got it, here you go!" 并提供 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)
方法可以重载,也就是说,只要参数不同,它们就可以有相同的名称(不允许只在return类型上重载)。
因此这是有效的:
void Foo(int x) { }
void Foo(char x) { }
或者,在您的情况下:
bool receive(int ID, double[] Bid, double[] Ask);
bool receive(int ID, out double first, out double second)
请注意,这是许多其他语言(包括 C++)的标准语言功能。
方法重载是面向对象的概念,其中方法可以具有相同的名称,只有参数(方法的签名)不同。
using System;
class Test
{
static void receive(int x, double y)
{
Console.WriteLine("receive(int x, double y)");
}
static void receive(double x, int y)
{
Console.WriteLine("receive(double x, int y)");
}
static void Main()
{
receive(5, 10.2);
}
}
看这里https://msdn.microsoft.com/en-us/library/aa691131(v=vs.71).aspx