Compiler Error CS1520

Method must have a return type

A method that is declared in a class, struct, or interface must have an explicit return type. In the following example, the Square method has a return value of string:

class Test
{
    string IntToString(int i)
    {
        return i.ToString();
    }
}

The following sample generates CS1520:

// CS1520a.cs
public class x
{
   // Method declaration missing a return type
   MyMethod()   // CS1520   
   {}
   // Add the desired return type:
   // void MyMethod2()
  // { }

   public static void Main()
   {
   }
}

Alternatively, this error might be encountered when the case of a constructor's name differs from that of the class or struct declaration, as in the following sample. Because the name is not exactly the same as the class name, the compiler interprets it as a regular method, not a constructor, and produces the error:

// CS1520b.cs
public class Class1
{
   // Should be Class1, not class1
   public class1()   // CS1520
   {
   }
   static void Main()
   {
   }
}

See Also

Reference

Methods (C# Programming Guide)

Constructors (C# Programming Guide)

Change History

Date

History

Reason

October 2008

Added text and code comments.

Customer feedback.