© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
7 replies
tama

Best practice for class constructors?

Hello there.
I'm creating a class with one private and one public variable. I am a little unsure how the constructor should work when this is the case. As an example:

public class A
{
  private readonly typeX _var1;
  public typeY var2Something { get; set; }
  
  public A(typeX var1, typeY var2) 
  {
    _var1 = var1;
    var2Something = var2;
  }
}
public class A
{
  private readonly typeX _var1;
  public typeY var2Something { get; set; }
  
  public A(typeX var1, typeY var2) 
  {
    _var1 = var1;
    var2Something = var2;
  }
}

Does it make sense to declare them first, and then assign them in the initializor? Also, are you supposed to add
{ get; set; }
{ get; set; }
to the class variable when it's also part of the constructor?
I am also unsure what the best practice is for variable names in C#. Some places I have seen names with capital first letter, other times I see something like
exampleVariable
exampleVariable
. I come from Python, where we use
_variable_name
_variable_name
for private variables, do you also use the underscore in C#?
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

✅ Function argument class reference / clone (best practice(s))
C#CC# / help
9mo ago
✅ Best practice when using HttpClient in a class
C#CC# / help
3y ago
❔ best design practice for this?
C#CC# / help
4y ago