C
C#3y ago
Grin

How do I create a class with an array within it?

I'm trying to create a class for people with an array of hobbies within it, but cannot figure out how to do that, can anyone tell me how I can go about doing this? internal class person { public string name { get; set; } public int age { get; set; } public int numberOfHobbies { get; set; } //how do I do an array? //public string[numberOfHobbies] hobbies { get; set; } }
3 Replies
Pobiega
Pobiega3y ago
public string[] Hobbies { get; set; }
Grin
GrinOP3y ago
I'm getting an error after myContacts.Add(new person { name = "Ian", age = 27, numberOfHobbies = 3, hobbies = { "basketball", "Soccer", "Golf"} }); the errors are the "basketball", "Soccer", "Golf" part
Pobiega
Pobiega3y ago
var person = new Person()
{
Hobbies = new[] { "derp" }
};
var person = new Person()
{
Hobbies = new[] { "derp" }
};
public class Person
{
public string Name { get; set; }
public int Age { get; set; } // date of birth instead?
public int NumberOfHobbies => Hobbies.Length;
public string[] Hobbies { get; set; }
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; } // date of birth instead?
public int NumberOfHobbies => Hobbies.Length;
public string[] Hobbies { get; set; }
}
Want results from more Discord servers?
Add your server