Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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.

Error:

Expected class, delegate, enum, interface, or struct

Function (and class):

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.

So, I assume this means string is not a class, delegate, enum, interface, or struct.

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!

like image 617
Peach Avatar asked Jan 28 '11 02:01

Peach


2 Answers

You need to put the method definition into a class/struct definition. Method definitions can't appear outside those.

like image 73
Femaref Avatar answered Oct 31 '22 21:10

Femaref


There is a capital S String in C#/.Net - System.String. But that is not your problem. @Femaref got it right - this error is indicating that your method is not part of a class.

C# does not support standalone functions, like C++ does. All methods have to be declared within the body of a class, interface or struct definition.

like image 4
Franci Penov Avatar answered Oct 31 '22 21:10

Franci Penov