© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
264 replies
Mek

✅ getting length of a list???

In my database file, I have a function that loads all of the ghost items back into a list and returns it
public List<Ghost>? LoadGhosts()
{
    var ghostInfo = File.ReadAllText(dbFile);
    List<Ghost> ghosts = new List<Ghost>();
            
    foreach (Ghost ghost in ghosts)
    {
        Ghost g = JsonSerializer.Deserialize<Ghost>(ghostInfo);
        ghosts.Add(g);
    }

    return ghosts;
}
public List<Ghost>? LoadGhosts()
{
    var ghostInfo = File.ReadAllText(dbFile);
    List<Ghost> ghosts = new List<Ghost>();
            
    foreach (Ghost ghost in ghosts)
    {
        Ghost g = JsonSerializer.Deserialize<Ghost>(ghostInfo);
        ghosts.Add(g);
    }

    return ghosts;
}
and in my game file, I am obtaining that list and attempting to get the length of it
public static void PlayGame(string selectedDiff, string username, DateTime date)
{
    Database db = new Database();
    var ghosts = db.LoadGhosts();
    int randGhostIndex = Helpers.RandomNumber(0, ghosts.Length);
}
public static void PlayGame(string selectedDiff, string username, DateTime date)
{
    Database db = new Database();
    var ghosts = db.LoadGhosts();
    int randGhostIndex = Helpers.RandomNumber(0, ghosts.Length);
}
I'm passing the length of the list to my random number generator, however, it says that ghosts doesn't have the Length property, so how do I get the length of the ghosts list to pass to my random number generator?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

✅ Getting duplicates out of a list
C#CC# / help
16mo ago
troubles with list length
C#CC# / help
4y ago
Getting the result from a list of tasks
C#CC# / help
4y ago
❔ Get list of indices from a List
C#CC# / help
3y ago