C# 如何将静态数组的元素传递给非静态方法?

C# 如何将静态数组的元素传递给非静态方法?,c#,static,C#,Static,我有一个静态数组,需要将其中的任意元素传递给非静态方法 我该怎么做 public class MyClass { public static int[] staticArray = { 3, 11, 43, 683, 2731 }; public void SomeMethod(int value) { //...stuff... } public static void staticMethod() { So

我有一个静态数组,需要将其中的任意元素传递给非静态方法

我该怎么做

public class MyClass
{
    public static int[] staticArray = { 3, 11, 43, 683, 2731 };

    public void SomeMethod(int value)
    {
        //...stuff...
    }

    public static void staticMethod()
    {
         SomeMethod(staticArray[2]);    //error here
    }
}

当我尝试类似的操作时,我得到了错误
非静态字段、方法或属性需要对象引用

您的代码是正确的,但是
'非静态字段、方法或属性需要对象引用'
在您尝试调用
实例
方法时发生,或者访问非静态字段/属性,而不是类的实例,比如从静态方法。例如:

class MyClass
{
    private int imNotStatic;

    public static void Bar()
    {
        // This will give you your 'An object reference is required` compile 
        // error, since you are trying to call the instance method SomeMethod
        // from a static method, as there is no 'this' to call SomeMethod on.
        SomeMethod(5);

        // This will also give you that error, as you are calling SomeMethod as
        // if it were a static method.
        MyClass.SomeMethod(42);

        // Again, same error, there is no 'this' to read imNotStatic from.
        imNotStatic = -1;
    }

    public void SomeMethod(int x)
    {
        // Stuff
    }
}

确保您没有执行上述操作之一。您确定要从构造函数调用
SomeMethod
吗?

您的代码很好,但是当您尝试调用
实例
方法时,非静态字段、方法或属性需要对象引用,或者访问非静态字段/属性,而不是类的实例,比如从静态方法。例如:

class MyClass
{
    private int imNotStatic;

    public static void Bar()
    {
        // This will give you your 'An object reference is required` compile 
        // error, since you are trying to call the instance method SomeMethod
        // from a static method, as there is no 'this' to call SomeMethod on.
        SomeMethod(5);

        // This will also give you that error, as you are calling SomeMethod as
        // if it were a static method.
        MyClass.SomeMethod(42);

        // Again, same error, there is no 'this' to read imNotStatic from.
        imNotStatic = -1;
    }

    public void SomeMethod(int x)
    {
        // Stuff
    }
}


确保您没有执行上述操作之一。您确定要从构造函数调用
SomeMethod
吗?

以上代码需要编译(如果你用
/…stuff…
替换
/…stuff…
。你的代码在LinqPad中对我来说很好。你一定太简化了你的测试。该代码编译得非常好。粘贴它的其余部分。错误必须在…stuff…的其他地方,或者在某个地方…对我来说运行良好使用了断点,
43
是有价值的。)正如预期的那样。@Albin,OP在编译时没有遇到问题。上面的代码可以编译(如果你用
/…stuff…
替换
/…stuff…
。你的代码在LinqPad中对我来说很好。你一定太简化了你的测试。该代码编译得非常好。粘贴它的其余部分。错误必须在…stuff…的其他地方,或者在某个地方…对我来说运行良好使用了断点,
43
是有价值的。)e正如预期的那样。@Albin,OP在编译时没有遇到问题。我似乎正是这样做的……请看我编辑的原始帖子。那么,我如何才能在没有错误的情况下完成我需要的工作呢?DaveYou不能,
SomeMethod
需要是静态的,您才能从静态方法调用它。请刷新静态和实例我之间的区别但是,请注意,在
SomeMethod
中,您可以访问
staticArray
,这意味着您可以编写
int value=staticArray[2],而不是
//stuff
但是如果我不能从
staticMethod
调用
SomeMethod
,当另一个表单调用
staticMethod
时,我就无法访问
int value=staticArray[2];
这里的目的是允许应用程序中的其他表单使用
MyClass.staticMethod()
运行
SomeMethod
,它通过非静态的道具/方法对
MyClass
产生影响。这一目的意味着误解了
静态的
实例的不同之处。假设在你的程序中调用
MyClass a;MyClass b;
时调用
MyClass.staticMethod()
它会调用什么实例?a,b?在静态方法中没有MyClass的实例可以调用非静态的道具/方法…请看我编辑的原始帖子。那么,我如何才能在没有错误的情况下做我需要的事?DaveYou不能,
SomeMethod
需要是静态的ic允许您从静态方法调用它。请刷新静态方法和实例方法之间的差异,希望这能让您深入了解解决实际问题的正确方法。但是,请注意,在
SomeMethod
中,您可以访问
staticArray
,而不是
/st>uff
你可以写
int-value=staticArray[2];
但是如果我不能从
staticMethod
调用
SomeMethod
我就无法访问
int-value=staticArray[2];
当另一个表单调用
staticMethod
时。这里的目的是允许应用程序中的其他表单使用
MyClass.staticMethod()
运行
SomeMethod
,它通过非静态的道具/方法对
MyClass
产生影响。这一目的意味着误解了
静态的
实例的不同之处。假设在你的程序中调用
MyClass a;MyClass b;
时调用
MyClass.staticMethod()
它会调用什么实例?a,b?在静态方法中没有MyClass的实例可以调用非静态的props/methods