© 2026 Hedgehog Software, LLC

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

✅ Learning C# through console app

// Program.cs
using PhasmoTriviaConsole.Services;

namespace PhasmoTriviaConsole;

class Program
{
    private Database _db;
    public Program(Database db)
    {
        _db = db;
        UserInfo UserName = new UserInfo(db.LoadUser());
    }

    public string UserName { get; set; }
}

// /Services/Database.cs
using System.Text.Json;

namespace PhasmoTriviaConsole.Services;

internal class Database
{
    private const string dbFile = "user.json";

    public UserInfo LoadUser()
    {
        if (File.Exists(dbFile))
        {
            var dbInfo = File.ReadAllText(dbFile);
            UserInfo info = JsonSerializer.Deserialize<UserInfo>(dbInfo);
            return info;
        }
        else
        {
            return new UserInfo();
        }
    }

    public void SaveUser(UserInfo info)
    {
        var content = JsonSerializer.Serialize(info);
        File.WriteAllText(dbFile, content);
    }
}

// /Models/UserModel.cs
namespace PhasmoTriviaConsole;

public class UserInfo
{
    public string Username { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string DOB { get; set; }
}
// Program.cs
using PhasmoTriviaConsole.Services;

namespace PhasmoTriviaConsole;

class Program
{
    private Database _db;
    public Program(Database db)
    {
        _db = db;
        UserInfo UserName = new UserInfo(db.LoadUser());
    }

    public string UserName { get; set; }
}

// /Services/Database.cs
using System.Text.Json;

namespace PhasmoTriviaConsole.Services;

internal class Database
{
    private const string dbFile = "user.json";

    public UserInfo LoadUser()
    {
        if (File.Exists(dbFile))
        {
            var dbInfo = File.ReadAllText(dbFile);
            UserInfo info = JsonSerializer.Deserialize<UserInfo>(dbInfo);
            return info;
        }
        else
        {
            return new UserInfo();
        }
    }

    public void SaveUser(UserInfo info)
    {
        var content = JsonSerializer.Serialize(info);
        File.WriteAllText(dbFile, content);
    }
}

// /Models/UserModel.cs
namespace PhasmoTriviaConsole;

public class UserInfo
{
    public string Username { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string DOB { get; set; }
}
I have decided to take a step back from Avalonia to just focus on learning C# through a console application. This is all I have in my console app. I cannot for the life of me figure out how to get access to the username stored within the UserInfo that is returned from the load function. Any suggestions would be great
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

✅ C# Console App Calculator Help
C#CC# / help
2mo ago
C# learning
C#CC# / help
11mo ago
LEARNING C#
C#CC# / help
14mo ago
learning C#
C#CC# / help
17mo ago