“预期的类、委托、枚举、接口或结构"公共静态字符串 MyFunc() 上的错误.“字符串"的替代方法是什么? [英] "Expected class, delegate, enum, interface or struct" error on public static string MyFunc(). What's an alternative to "string"?
问题描述
当我尝试使用以下静态函数时出现错误.
I'm getting an error when I attempt to use the following static function.
错误:
预期的类、委托、枚举、接口或结构
Expected class, delegate, enum, interface, or struct
函数(和类):
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;
}
}
}
这导致编译器愤怒地(红色)在public static string
的字符串部分下划线.
This is causing the compiler to angrily (in red) underline the string part of public static string
.
所以,我认为这意味着 string
不是类、委托、枚举、接口或结构.
So, I assume this means string
is not a class, delegate, enum, interface, or struct.
我可以用什么代替 string
来返回字符串或类似字符串的对象? 似乎没有 String
C# 中的(大写 S)类.
What can I use instead of string
to return a string or string-like object? There doesn't appear to be a String
(capital S) class in C#.
编辑:括号与一些注释代码不匹配 - 上面的代码工作正常,我的实际不匹配代码没有.谢谢!
Edit: Bracket mis-match with some commented code - the above code works correctly, my actual mis-matched code didn't. Thanks!
推荐答案
您需要将方法定义放入类/结构体定义中.方法定义不能出现在这些之外.
You need to put the method definition into a class/struct definition. Method definitions can't appear outside those.
这篇关于“预期的类、委托、枚举、接口或结构"公共静态字符串 MyFunc() 上的错误.“字符串"的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!