© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•12mo ago•
26 replies
Moebytes

class constructor in c#

My editor wants to convert the following code
public class Ping : ICommand {
    private readonly DiscordClient discord;
    private readonly SocketUserMessage message;

    public Ping(DiscordClient discord, SocketUserMessage message) {
        this.discord = discord;
        this.message = message;
    }
}
public class Ping : ICommand {
    private readonly DiscordClient discord;
    private readonly SocketUserMessage message;

    public Ping(DiscordClient discord, SocketUserMessage message) {
        this.discord = discord;
        this.message = message;
    }
}

to this
public class Ping(DiscordClient discord, SocketUserMessage message) : ICommand {
    private readonly DiscordClient discord = discord;
    private readonly SocketUserMessage message = message;
}
public class Ping(DiscordClient discord, SocketUserMessage message) : ICommand {
    private readonly DiscordClient discord = discord;
    private readonly SocketUserMessage message = message;
}

is this the recommended way to write the constructor in c#? And do I still need the
private readonly DiscordClient discord = discord;
private readonly DiscordClient discord = discord;
part or does that get automatically assigned.
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

✅ ✅ Static constructor for C# class not being called
C#CC# / help
3y ago
✅ Public class in C#
C#CC# / help
13mo ago
c# class
C#CC# / help
3y ago
Init Timer in class with Primary Constructor?
C#CC# / help
7mo ago