Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

stephen Capes
stephen Capes
1,002 Points

error CS1520: Class, struct, or interface method must have a return type.

I've gone through the whole thing multiple times and tried different things and can't find a problem please help as I cant move on.

Emmanuel C
Emmanuel C
10,636 Points

Can you post your code please? You can enclose to the code in 3 backticks to format the code. Check the Markdown Cheatsheet for how its done.

2 Answers

Allan Clark
Allan Clark
10,810 Points

Code would definitely help here to pinpoint where the issue is, but just going by the error it sounds like your syntax for a method signature is a little off. Remember, a method signature (the stuff on the first line of the method) needs to have a few things, and they need to be in the exact correct order as follows (there are other keywords that can be added but this is the bare minimum):

[access modifier] [return type] [method name]([parameters if any])

//here is a proper method with correct signature
public int GetAnswerToUltimateQuestion()
{
     return 42;
}

The provided error indicates that the return type is missing, in this case it would be missing the 'int' keyword. So just to illustrate, the compiler is telling you that somewhere in your code there is a method that is improperly formed such as below:

//this code will not compile because it doesn't know what type the method will return
public GetAnswerToUltimateQuestion()
{
     return 42;
}

Hope this helps! Happy Coding!

Steven Parker
Steven Parker
225,652 Points

In case Allan's answer doesn't fit the issue, I'll take a wild guess at another possibility:

You might get this message from a misspelled constructor method. It's normal for a constructor to not have a return type, but it's important that the constructor's name is exactly the same as the class itself.

So check for a typo or misspelling in the constructor name. Let us know if you've resolved it, or share the code here if you still need help.