C
C#2w ago
Faker

✅ What is a primary constructor in C#

Hello guys, was just reading a bit about primary constructors. My question is, primary constructors is just a "fancy" way to write less code instead of declaring a whole constructor? Or are there any reasons why we would use primary constructors?
16 Replies
Thinker
Thinker2w ago
Yes, primary constructors are literally just a simpler way to write normal constructors. These two are literally equivalent
class Person(string name, int age)
{
public string Name { get; } = name;
public int Age { get; } = age;
}
class Person(string name, int age)
{
public string Name { get; } = name;
public int Age { get; } = age;
}
class Person
{
public string Name { get; }
public int Age { get; }

public Person(int name, int age)
{
Name = name;
Age = age;
}
}
class Person
{
public string Name { get; }
public int Age { get; }

public Person(int name, int age)
{
Name = name;
Age = age;
}
}
If the constructor is public and you don't need any kind of functionality in the constructor besides initializing data, then a primary constructor is the same.
Faker
FakerOP2w ago
yep I see
Thinker
Thinker2w ago
-# There are some slight nuances, but those are quite fringe.
Angius
Angius2w ago
Now, mind you, what the primary ctor generates can differ Depending on where you capture those params
Faker
FakerOP2w ago
yeah the idea of backing field, etc ?
Angius
Angius2w ago
But most of the time it doesn't really matter ye
Thinker
Thinker2w ago
oh yeah, right, you can reference the parameters from the primary constructor elsewhere in the class eg. you can do this
class Foo(int num)
{
public void PrintNum()
{
Console.WriteLine(num);
}
}
class Foo(int num)
{
public void PrintNum()
{
Console.WriteLine(num);
}
}
Faker
FakerOP2w ago
yeah I see, yeah I see, hmm, consider the code: when we use primary constructors, like here:
C#
class Person(string name, int age)
{
public string Name { get; } = name;
public int Age { get; } = age;
}
C#
class Person(string name, int age)
{
public string Name { get; } = name;
public int Age { get; } = age;
}
the syntax is to set the variable "name" used in the primary constructors to the property "Name"?
Thinker
Thinker2w ago
Reverse, the property Name is set to the parameter name
Faker
FakerOP2w ago
yeah sorry
Angius
Angius2w ago
You can always use Sharplab to see what gets generated
Faker
FakerOP2w ago
yeah I see
Thinker
Thinker2w ago
Mind you again that primary constructors in records actually declare properties, which primary constructors in normal classes do not
Angius
Angius2w ago
And that nobody really writes record class when you can use just record 😛
Thinker
Thinker2w ago
in record L(string Z);, Z will become a property, but in class L(string z); you'll get an empty class with just a constructor It's somewhat odd, but you don't need to worry about it too much unless you already know what records are.
Faker
FakerOP2w ago
just learning about that :c yep, I guess if I have some doubts, will just stick with sharplab to clearly see what's happening But I have an overview now, thanks guys 👍

Did you find this page helpful?