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

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

问题描述

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

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 来返回字符串或类似字符串的对象? 似乎没有 StringC# 中的(大写 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屋!

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