The object of you affection project

This is my class Profile page

the code works fine but only prints bio and the first hobby. Any idea what I’m doing wrong? I want the rest of the array hobbies to be printed.

Hello! Here:

foreach (string hobby in hobbies)
      {return bio += $" - {hobby}\n";}

You return the result, which means the loop will exit after only one iteration (since return exits the function, and any loop within it). Does the viewProfile() method need to return anything?

I have a problem with this project. I have a strange error message that change everytime but I don’t touch the code. The error is:

Profile.cs(29,19): error CS0161: ‘Profile.ViewProfile()’: not all code paths return a value [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]

But sometimes, the error is at the line 21, and I don’t know why… Can you tell me what’s wrong with my programm ?

using System;

namespace DatingProfile


  class Profile {

    private string name;

    private int age;

    private string city;

    private string country;

    private string pronouns;

    private string[] hobbies;

    private int Age {

      get {return age;}

      set {

        if (value < 18) {age = 18;}

        else {age = value;}



    public Profile(string name, int age, string city = "Unknown", string country = "Unknown", string pronouns = "they/them") { = name;

      this.age = age; = city; = country;

      this.pronouns = pronouns;


    public string ViewProfile() {

      string bio = $"Hello!\nMy name is {name}\nI have {age} years old\nI live in {city} in {country}\nI refer as {pronouns}\n";

      if (hobbies.Length < 0) {

        bio += "My hobbies are:\n";

      foreach (string h in this.hobbies) {

        bio += $"_{h}\n";



      return bio;



    public void SetHobbies(string[] hobbies) {

      this.hobbies = hobbies;




Hello, @jordykonpyta .

Look closely at your ViewProfile() method. You specify that it returns a string. The error tells you that not all paths in the method return a value. What if hobbies.length isn’t less than 0?

Hello @midlindner

Ah, ok.

I tried to add a else that contains returns 0; but now I’ve got those errors :

Profile.cs(36,9): error CS8641: ‘else’ cannot start a statement. [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(36,9): error CS1003: Syntax error, ‘(’ expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(36,9): error CS1525: Invalid expression term ‘else’ [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(36,9): error CS1026: ) expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(36,9): error CS1002: ; expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]

I don’t understand this project… Everything was fine in the last 10 seconds, then I try to do step 12 and everything is broken…

All is not lost. You simply have your return statement nested inside the if block. You’ll want to return the bio whether or not there are any hobbies to add, so move the return statement to be the last line of code in the method. Also, you probably want to add hobbies only if there are any.

if (hobbies.Length < 0)
// the length of an array cannot ever be less than 0. If you want to know whether or not 
// the array contains 1 or more elements, you'd want to see if the length was greater than zero

Ah, okay ! I didn’t notice, I’m stupid ^^’

It works fine now ! Thank you very much !

1 Like

You’re welcome.