The exercise is The-Object-of-Your-Affection. When I try to loop through the array hobbies to format it the way I want I get the error CS0165: Use of unassigned local variable ‘hobby’. Here is the code I have so far:
Program.cs
using System;
namespace DatingProfile
{
class Program
{
static void Main(string[] args)
{
Profile sam = new Profile(name: "Sam Drakkila", age: 30, city: "New York", country: "USA", pronouns: "he/him");
string[] h = {"stuff", "things", "whatnot"};
sam.SetHobbies(h);
Console.WriteLine(sam.ViewProfile());
}
}
}
Profile.cs
The error is in this file. The method starts on line 29.
using System;
namespace DatingProfile
{
class Profile{
// Fields
private string name;
private int age;
private string city;
private string country;
private string pronouns;
private string[] hobbies;
// Constructors
public Profile(string name, int age, string city, string country, string pronouns = "they/them") {
this.name = name;
this.age = age;
this.city = city;
this.country = country;
this.pronouns = pronouns;
this.hobbies = new string[0];
}
// public methods
public string ViewProfile(){
string hobby;
foreach (string i in this.hobbies){
hobby += $"o {i}\n";
}
string info = $"{this.name} is {this.age} and lives in {this.city}, {this.country}. {this.name}'s pronouns are {this.pronouns}.\nThere hobbies are:\n{hobby}";
return info;
}
public void SetHobbies(string[] hobbies){
this.hobbies = hobbies;
}
}
}