C# &引用;应为类、委托、枚举、接口或结构;公共静态字符串MyFunc()出错。什么';“是”的替代品;字符串“;?

C# &引用;应为类、委托、枚举、接口或结构;公共静态字符串MyFunc()出错。什么';“是”的替代品;字符串“;?,c#,compiler-errors,static-methods,C#,Compiler Errors,Static Methods,当我尝试使用以下静态函数时,出现了一个错误 错误: 应为类、委托、枚举、接口或结构 功能(和类别): 这会导致编译器在公共静态字符串的字符串部分下划红色下划线 因此,我假设这意味着string不是类、委托、枚举、接口或结构 我可以使用什么来代替string来返回字符串或类似字符串的对象?在C#中似乎没有string(大写字母S)类 编辑:括号与一些注释代码不匹配-上述代码工作正常,但我实际的不匹配代码没有。谢谢 您需要将方法定义放入类/结构定义中。方法定义不能出现在这些定义之外。在C#/.Net

当我尝试使用以下静态函数时,出现了一个错误

错误:

应为类、委托、枚举、接口或结构

功能(和类别):

这会导致编译器在
公共静态字符串的字符串部分下划红色下划线

因此,我假设这意味着
string
不是类、委托、枚举、接口或结构

我可以使用什么来代替
string
来返回字符串或类似字符串的对象?
在C#中似乎没有
string
(大写字母S)类


编辑:括号与一些注释代码不匹配-上述代码工作正常,但我实际的不匹配代码没有。谢谢

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

在C#/.Net-中有一个大写的S字符串。但这不是你的问题@Femaref做对了-此错误表示您的方法不是类的一部分


C不支持独立的函数,如C++。所有方法都必须在类、接口或结构定义的主体内声明。

我在重新熟悉p-Invoke时遇到了这个问题。做对了。以下是一些用于快速可视化的示例代码:

工作代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices; 

namespace ConsoleApplication2
{
    class Program
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetModuleHandle(string lpModuleName);

        static void Main(string[] args)
        {

        }
    }
}
破译代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr GetModuleHandle(string lpModuleName);

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

        }
    }
}

我很确定它不是,至少我没有遇到这个特殊的编译器错误。检查你的牙套,这可能也是一个原因。完全正确。直到更好地解释之后,才发现括号不匹配。谢谢你的澄清。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr GetModuleHandle(string lpModuleName);

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

        }
    }
}