-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C# Constructor Shorthand #3279
Comments
Those other "large and more comprehensive changes" completely cover what you are proposing here though. So this is just a syntax variation on what is already in planning for v9. So this is really just a duplicate request. |
That I did not know. Thanks! All I am saying is that something has to be done. |
This is not records but primary constructors, which also have their own proposal. #2691 |
@Richiban Eh, primary constructors let you say: public class C(int i, string s)
{
...
} which isn't what this issue says. The idea of primary constructors is an older one which sort of got subsumed into the records proposal, if I'm following things correctly. #2691 is in the list of issues I gave as references, either way. |
@canton7 The syntax is slightly different, but the feature is the same--my class has a dependency (a field), let me write it once in such a way that it gets set in a constructor. Records are a quick and clean way of defining a class with value equality and, depending on which version of records you go for, an auto-generated constructor. |
@Richiban They keep humming and harring about whether value equality is part of records, or its own separate thing. I think it's safe to say that all discussion around the idea of "let me declare all of these properties, and a constructor to assign to them, with minimal effort" come under the heading of "Records" these days. |
It looks like I hate the records already... The records as I understude are structures with simplified construction. I am not so deep in the language specification to say something about theese things. I have't felt necessity in them. But I need a simplified construction of complex class with DI. Surely I will never compare the classes by theese properties. So a vary important problem of records is not important in this case. |
@yulObraz Records are a quick and easy way to write DTO classes and has nothing to do with this issue. This issue is to get the same shorthand that Typescript has. I think records are inspired from F#. |
It now seems pretty possible that C# will get primary constructors (ironically enough they talk about it in a records LDM notes: https://github.com/dotnet/csharplang/blob/master/meetings/2020/LDM-2020-04-27.md) |
Primary constructors will allow something similar to this: public class C(ServiceA a, ServiceB b)
{
public void M()
{
a.DoSomething();
b.DoSomethingElse();
}
} |
Closing, as this is being considered as part of the discussion surrounding primary constructors: #2691 |
Much like the Typescript language, it would be nice to get rid of the tedious writing of constructors when using dependency injection.
Today I will write something like:
My suggestion is that we could write something like this in the future:
The text was updated successfully, but these errors were encountered: