Improve struct constructor to make it work like class constructor #3420
Unanswered
KorneiDontsov
asked this question in
Language Ideas
Replies: 2 comments
-
Note that there is a workaround for this limitation: public struct Struct
{
public int Int;
public double Double;
public string String;
public Struct(int integer)
{
this = default;
Int = integer;
}
} It is a bit ugly, but it does save on typing. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Another workaround, arguably one that is less ugly, is to explicitly delegate to struct S
{
public int Int;
public double Double;
public string String;
public S(int integer) : this()
{
Int = integer;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When I use
struct
and wrote parameterized constructor, I must initialize every field with a value, even if it is going to bedefault
.But in
class
I doesn't have to do it. Field isdefault
/null
by default.It's annoying that in
struct
I have to wrote even more code than inclass
in order to do exactly same thing. I propose to make the compiler do the work even if implementation of howstruct
constructor works is different from howclass
constructor works.Examples
Beta Was this translation helpful? Give feedback.
All reactions