C# 为什么下面的代码是错误的?5.

C# 为什么下面的代码是错误的?5.,c#,C#,您试图从无效的静态方法调用实例方法。您还必须从方法返回一个字符串,因为调用方需要它 将其更改为: class Program { static void Main(string[] args) { string s= sum("jfjf"); } void sum(string xx) { Console.WriteLine(xx); } } 您的方法返回void,这意味着什么也没有,但您正在尝试将其分配给

您试图从无效的静态方法调用实例方法。您还必须从方法返回一个字符串,因为调用方需要它

将其更改为:

class Program
{
    static void Main(string[] args)
    {
       string s= sum("jfjf");

    }


     void sum(string xx)
    {
        Console.WriteLine(xx);

    }
}
您的方法返回void,这意味着什么也没有,但您正在尝试将其分配给字符串

更改方法定义,如下所示:

class Program
{
    static void Main(string[] args)
    {
       string s= sum("jfjf");

    }


    static string sum(string xx)
    {
        Console.WriteLine(xx);
        return xx;

    }
}
或者不尝试分配它,只需调用该方法:

static string sum(string xx)
{
    Console.WriteLine(xx);
    return xx;
}

您也应该将其设置为静态,因为您的主方法是静态的。您不能在静态上下文中调用非静态方法。

它有什么问题?存在多个编译器错误,其中一个是void方法没有返回值,因此无法在表达式中使用,但请阅读错误并在帖子中逐字包含此类错误消息/症状。静态字符串sumstring xxThank jjbhhjbjError 1无法隐式将类型“void”转换为“string”
sum("jfjf");