C
C#3mo ago
tommy

✅ what is a primary constructor? is it any similar to parameterless constructor?

the content provided on Microsoft Learn is confusing.
80 Replies
Pobiega
Pobiega3mo ago
Are you familiar with records?
public record Example(string Name);
public record Example(string Name);
?
tommy
tommyOP3mo ago
hey man!! how are you? not particularly
Pobiega
Pobiega3mo ago
okay, lets ignore them for now then
tommy
tommyOP3mo ago
do i have to know them in order to understand this
Pobiega
Pobiega3mo ago
nope
tommy
tommyOP3mo ago
okay
Pobiega
Pobiega3mo ago
a primary constructor is just a different way of writing a constructor - but it then becomes the "primary" constructor that all other constructors must also use
public class Example(string name)
{
public string Name { get; } = name;
}
public class Example(string name)
{
public string Name { get; } = name;
}
simple example, the primary constructor takes in a single string name that we use to initialize a property.
tommy
tommyOP3mo ago
hold on so we define it along with the class name and there's a parameter available throughout the scope of the class that is provided inside that constructor
Pobiega
Pobiega3mo ago
yes
tommy
tommyOP3mo ago
okay! now let's get to the second part when you say
Pobiega
Pobiega3mo ago
okay, lets introduce a non-primary constructor here
tommy
tommyOP3mo ago
"all other constructor must also use"
Pobiega
Pobiega3mo ago
ye
public class Example(string name)
{
public string Name { get; } = name;

// parameterless constructor
public Example() : this("Default")
{
}

public Example(string name, int age) : this(name)
{
Age = age;
}

public int Age { get; private set; }
}
public class Example(string name)
{
public string Name { get; } = name;

// parameterless constructor
public Example() : this("Default")
{
}

public Example(string name, int age) : this(name)
{
Age = age;
}

public int Age { get; private set; }
}
here we have the same class but with 2 alternative constructors
tommy
tommyOP3mo ago
okay!
Pobiega
Pobiega3mo ago
we add a parameterless one, that just sets the name to a default value, and one that also sets an age they must call this(string) or the compiler will be angry
tommy
tommyOP3mo ago
so, here, this refers to the primary constructor? i mean this
Pobiega
Pobiega3mo ago
yes its not different from constructor overloading in general, except that its a must here
tommy
tommyOP3mo ago
okay! so you gotta have a parameterless constructor inside the class
Pobiega
Pobiega3mo ago
no? I just added that as an example
tommy
tommyOP3mo ago
then why does the compiler get angry
Pobiega
Pobiega3mo ago
No description
tommy
tommyOP3mo ago
oh i see!
Pobiega
Pobiega3mo ago
the primary constructor is primary
tommy
tommyOP3mo ago
every constructor must use the primary constructor
Pobiega
Pobiega3mo ago
you cant skip invoking it yes or use one that uses it, iirc
tommy
tommyOP3mo ago
so there doesn't have to be a primary constructor but if there is one, every other constructor must follow it
Pobiega
Pobiega3mo ago
yes primary ctors are optional.
tommy
tommyOP3mo ago
what if i make the primary constructor that is parameterless
Pobiega
Pobiega3mo ago
thats the default. its called "not having a primary constructor" 😛
tommy
tommyOP3mo ago
same thing goes for it as well?
Pobiega
Pobiega3mo ago
note how the primary constructor cant have a body, it just has parameters
tommy
tommyOP3mo ago
does it have to have them?
Pobiega
Pobiega3mo ago
so if you want a parameterless constructor with a body, just make a normal constructor I mean, what is a parameter less primary constructor? its literally nothing
tommy
tommyOP3mo ago
yeah!
Pobiega
Pobiega3mo ago
primary ctors do one thing and ONE THING ONLY: they specify parameters so a parameterless primary ctor is... pointless
tommy
tommyOP3mo ago
very quirky question but, do we still have to follow the this() protocol even when the primary constructor is paramterless? yeah i get it
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3mo ago
No description
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
sorry i had to go to pee yeah so having an empty primary constructor is not possible then, right? i think weird questions do help us better understand the language
Pobiega
Pobiega3mo ago
it is, but its useless
public class Example()
{
public string Name { get; }
}
public class Example()
{
public string Name { get; }
}
this is valid, but why bother? just confuses people
tommy
tommyOP3mo ago
got it one more thing are the get and set access methods public by default
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
okay
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
yeah i saw this earlier
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
from pobiega
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
ookay
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
ohhh
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
got it
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
i haven't yet got to async and await but the rest of the thing
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
i understood that was a really helpful discussion thank you
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
i will have to learn that first lol thank you to both @TeBeCo and @Pobiega
Pobiega
Pobiega3mo ago
np
tommy
tommyOP3mo ago
this does not seem to be the case i tried but the property remains private i think or maybe i am wrong idk
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
ohhh got it it does not have a setter shit
MODiX
MODiX3mo ago
TeBeCo
REPL Result: Success
var foo = Foo.Create();
Console.WriteLine(foo.Name);

public class Foo
{
internal Foo() {}

public string Name { get; } = "bla";

public static Foo Create() => new();
}
var foo = Foo.Create();
Console.WriteLine(foo.Name);

public class Foo
{
internal Foo() {}

public string Name { get; } = "bla";

public static Foo Create() => new();
}
Console Output
bla
bla
Compile: 496.598ms | Execution: 35.344ms | React with ❌ to remove this embed.
tommy
tommyOP3mo ago
yeah my bad my bad sorry no i think it is man check this out
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
as i hover over it in visual studio it says private
tommy
tommyOP3mo ago
No description
tommy
tommyOP3mo ago
this is it there seems to be a problem here
Pobiega
Pobiega3mo ago
uh yeah, thats private. members are by default private there is no problem, this is the expected behaviour
tommy
tommyOP3mo ago
what is up with this compiler then
Pobiega
Pobiega3mo ago
?
tommy
tommyOP3mo ago
this is confusing it compiled
Pobiega
Pobiega3mo ago
so? I dont see a problem the property is public
tommy
tommyOP3mo ago
ohh okay
Pobiega
Pobiega3mo ago
the constructor is internal, but called via a public static method
tommy
tommyOP3mo ago
i thought there wasn't an access modifier got it!!
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP3mo ago
got it!!
Want results from more Discord servers?
Add your server