C
C#2mo 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
Pobiega2mo ago
Are you familiar with records?
public record Example(string Name);
public record Example(string Name);
?
tommy
tommyOP2mo ago
hey man!! how are you? not particularly
Pobiega
Pobiega2mo ago
okay, lets ignore them for now then
tommy
tommyOP2mo ago
do i have to know them in order to understand this
Pobiega
Pobiega2mo ago
nope
tommy
tommyOP2mo ago
okay
Pobiega
Pobiega2mo 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
tommyOP2mo 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
Pobiega2mo ago
yes
tommy
tommyOP2mo ago
okay! now let's get to the second part when you say
Pobiega
Pobiega2mo ago
okay, lets introduce a non-primary constructor here
tommy
tommyOP2mo ago
"all other constructor must also use"
Pobiega
Pobiega2mo 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
tommyOP2mo ago
okay!
Pobiega
Pobiega2mo 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
tommyOP2mo ago
so, here, this refers to the primary constructor? i mean this
Pobiega
Pobiega2mo ago
yes its not different from constructor overloading in general, except that its a must here
tommy
tommyOP2mo ago
okay! so you gotta have a parameterless constructor inside the class
Pobiega
Pobiega2mo ago
no? I just added that as an example
tommy
tommyOP2mo ago
then why does the compiler get angry
Pobiega
Pobiega2mo ago
No description
tommy
tommyOP2mo ago
oh i see!
Pobiega
Pobiega2mo ago
the primary constructor is primary
tommy
tommyOP2mo ago
every constructor must use the primary constructor
Pobiega
Pobiega2mo ago
you cant skip invoking it yes or use one that uses it, iirc
tommy
tommyOP2mo ago
so there doesn't have to be a primary constructor but if there is one, every other constructor must follow it
Pobiega
Pobiega2mo ago
yes primary ctors are optional.
tommy
tommyOP2mo ago
what if i make the primary constructor that is parameterless
Pobiega
Pobiega2mo ago
thats the default. its called "not having a primary constructor" 😛
tommy
tommyOP2mo ago
same thing goes for it as well?
Pobiega
Pobiega2mo ago
note how the primary constructor cant have a body, it just has parameters
tommy
tommyOP2mo ago
does it have to have them?
Pobiega
Pobiega2mo 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
tommyOP2mo ago
yeah!
Pobiega
Pobiega2mo ago
primary ctors do one thing and ONE THING ONLY: they specify parameters so a parameterless primary ctor is... pointless
tommy
tommyOP2mo 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 User2mo ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega2mo ago
No description
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo 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
Pobiega2mo 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
tommyOP2mo ago
got it one more thing are the get and set access methods public by default
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
okay
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
yeah i saw this earlier
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
from pobiega
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
ookay
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
ohhh
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
got it
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
i haven't yet got to async and await but the rest of the thing
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
i understood that was a really helpful discussion thank you
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
i will have to learn that first lol thank you to both @TeBeCo and @Pobiega
Pobiega
Pobiega2mo ago
np
tommy
tommyOP2mo 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 User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
ohhh got it it does not have a setter shit
MODiX
MODiX2mo 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
tommyOP2mo ago
yeah my bad my bad sorry no i think it is man check this out
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
as i hover over it in visual studio it says private
tommy
tommyOP2mo ago
No description
tommy
tommyOP2mo ago
this is it there seems to be a problem here
Pobiega
Pobiega2mo ago
uh yeah, thats private. members are by default private there is no problem, this is the expected behaviour
tommy
tommyOP2mo ago
what is up with this compiler then
Pobiega
Pobiega2mo ago
?
tommy
tommyOP2mo ago
this is confusing it compiled
Pobiega
Pobiega2mo ago
so? I dont see a problem the property is public
tommy
tommyOP2mo ago
ohh okay
Pobiega
Pobiega2mo ago
the constructor is internal, but called via a public static method
tommy
tommyOP2mo ago
i thought there wasn't an access modifier got it!!
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
tommy
tommyOP2mo ago
got it!!
Want results from more Discord servers?
Add your server