"预期的类,委托,枚举,接口或struct& quot;公共静态字符串MyFunc()上的错误.什么是& quot;字符串& quot;的替代方案? [英] "Expected class, delegate, enum, interface or struct" error on public static string MyFunc(). What's an alternative to "string"?

查看:57
本文介绍了"预期的类,委托,枚举,接口或struct& quot;公共静态字符串MyFunc()上的错误.什么是& quot;字符串& quot;的替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用以下静态函数时出现错误.

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;
        }
    } 
}

这导致编译器愤怒地(红色)在公共静态字符串的字符串部分下划线.

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 (大写S)类在C#中.

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.

这篇关于"预期的类,委托,枚举,接口或struct& quot;公共静态字符串MyFunc()上的错误.什么是& quot;字符串& quot;的替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆