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