✅ what is a primary constructor? is it any similar to parameterless constructor?
the content provided on Microsoft Learn is confusing.
80 Replies
Are you familiar with records?
?
hey man!!
how are you?
not particularly
okay, lets ignore them for now then
do i have to know them in order to understand this
nope
okay
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
simple example, the primary constructor takes in a single string
name
that we use to initialize a property.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
yes
okay!
now let's get to the second part
when you say
okay, lets introduce a non-primary constructor here
"all other constructor must also use"
ye
here we have the same class but with 2 alternative constructors
okay!
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 angryso, here, this refers to the primary constructor?
i mean
this
yes
its not different from constructor overloading in general, except that its a must here
okay! so you gotta have a parameterless constructor inside the class
no?
I just added that as an example
then why does the compiler get angry
oh i see!
the primary constructor is primary
every constructor must use the primary constructor
you cant skip invoking it
yes
or use one that uses it, iirc
so there doesn't have to be a primary constructor but if there is one, every other constructor must follow it
yes
primary ctors are optional.
what if i make the primary constructor that is parameterless
thats the default. its called "not having a primary constructor" 😛
same thing goes for it as well?
note how the primary constructor cant have a body, it just has parameters
does it have to have them?
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
yeah!
primary ctors do one thing and ONE THING ONLY: they specify parameters
so a parameterless primary ctor is... pointless
very quirky question but, do we still have to follow the
this()
protocol even when the primary constructor is paramterless?
yeah i get itUnknown User•3w ago
Message Not Public
Sign In & Join Server To View
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
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
it is, but its useless
this is valid, but why bother?
just confuses people
got it
one more thing
are the get and set access methods public by default
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
okay
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
yeah i saw this earlier
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
from pobiega
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
ookay
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
ohhh
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
got it
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
i haven't yet got to async and await
but the rest of the thing
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
i understood
that was a really helpful discussion
thank you
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
i will have to learn that first
lol
thank you to both @TeBeCo and @Pobiega
np
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•3w ago
Message Not Public
Sign In & Join Server To View
ohhh
got it
it does not have a setter
shit
TeBeCo
REPL Result: Success
Console Output
Compile: 496.598ms | Execution: 35.344ms | React with ❌ to remove this embed.
yeah
my bad my bad
sorry
no
i think it is man
check this out
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
as i hover over it in visual studio
it says private
this is it
there seems to be a problem here
uh yeah, thats private.
members are by default private
there is no problem, this is the expected behaviour
what is up with this compiler then
?
this is confusing
it compiled
so?
I dont see a problem
the property is public
ohh
okay
the constructor is internal, but called via a public static method
i thought there wasn't an access modifier
got it!!
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
got it!!