© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•9mo ago•
19 replies
Foffs

✅ Does C# have anonymous struct initializers for class fields?

Hey, I'm new to C#. I have been refactoring the code I wrote so it looks cleaner.
I have something like this:
struct GeneralConfig(bool log, bool enabled) {
  public bool Log = log;
  public bool Enabled = enabled;
}

class Config(bool log) {
  public GeneralConfig General = new GeneralConfig (log, true);
// ...
}
struct GeneralConfig(bool log, bool enabled) {
  public bool Log = log;
  public bool Enabled = enabled;
}

class Config(bool log) {
  public GeneralConfig General = new GeneralConfig (log, true);
// ...
}

The code works but it has quite a bit of boilerplate, I was wondering if it's possible to declare the structure and initial values at the field definition.
Something like:
class Config(bool log) {
  public struct General = new {
    public bool Log = log;
    public bool Enabled = true;
  }
}
class Config(bool log) {
  public struct General = new {
    public bool Log = log;
    public bool Enabled = true;
  }
}

The above is invalid syntax, it's just to provide an idea of what I mean.

Thank you!
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

Similar Threads

❔ Class with Func<> and class initializers
C#CC# / help
3y ago
✅ Struct Fields
C#CC# / help
4y ago
❔ static fields for struct types
C#CC# / help
4y ago
❔ Struct vs Class
C#CC# / help
3y ago