公共静态字符串MyFunc()上的"预期的类,委托,枚举,接口或结构"错误.什么是"字符串"的替代品?

Pea*_*ach 12 c# static-methods compiler-errors

我尝试使用以下静态函数时收到错误.

错误:

预期的类,委托,枚举,接口或结构

功能(和类):

namespace MyNamespace
{
    public class MyClass
    {
        // Some other static methods that use Classes, delegates, enums, interfaces, or structs

        public static string MyFunc(string myVar){
            string myText = myVar;
            //Do some stuff with myText and myVar
            return myText;
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

这导致编译器愤怒地(红色)为字符串部分加下划线public static string.

所以,我认为这意味着string不是类,委托,枚举,接口或结构.

我可以使用什么而不是string返回字符串或类似字符串的对象?String在C#中似乎没有(大写S)类.

编辑:括号与某些注释代码不匹配 - 上面的代码工作正常,我的实际不匹配代码没有.谢谢!

Fem*_*ref 24

您需要将方法定义放入类/结构定义中.方法定义不能出现在那些之外.

  • 而且我很确定它不是,至少我没有遇到任何其他原因的特定编译器错误.检查你的牙套,这也是一个原因. (4认同)