这是一个代码示例,正在为我抛出编译器错误,我不知道如何解决。错误是“访问之前,本地变量NumArray可能未初始化”。我确实知道我正在使用旧的VB Utils CopyArray方法。想要解决该错误,并提供使用C#Array.Copy方法的解决方案。
public class Service
{
public Service()
{
RegisterIntents()
}
private void RegisterIntents()
{
// Find all classes derived from Service
// Find each method in those classes with the [IntentHandler]
// Attribute
// Get the method's Intent Class derived parameter type
// create a delegate I can invoke later for that method.
// Add the delegate to a Dictionary<Intent,Delegate>;
}
}
答案 0 :(得分:0)
您错误地声明了numArray
。正确的方法是:
int[] numArray = new int[] {};
C#中的数组是对象,因此在初始化它们时必须调用它们的ctor。
答案 1 :(得分:0)
问题可能是您忘了处理CopyArray
的返回值,它是新数组。
因此更改此行
((int[])Utils.CopyArray(numArray, new int[index + 1]))[index] = i;
对此:
numArray= (int[]) Utils.CopyArray(numArray, new int[index + 1]);
numArray[index] = i;
那是假设Utils.CopyArray
的样子。虽然我不确定为什么当我们都拥有Array.Copy
时,为什么还会有人使用它。