Skip to content
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

Closed
TropicalThunder opened this issue Mar 19, 2020 · 13 comments
Closed

C# Constructor Shorthand #3279

TropicalThunder opened this issue Mar 19, 2020 · 13 comments

Comments

@TropicalThunder
Copy link

TropicalThunder commented Mar 19, 2020

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:

public class TestClass
{
	private readonly DatabaseContext _context;

	public TestClass(DatabaseContext context)
	{
		_context = context;
	}  
}

My suggestion is that we could write something like this in the future:

public class TestClass
{
    public TestClass(private DatabaseContext context) { }  
}

@canton7
Copy link

canton7 commented Mar 19, 2020

This looks similar to records. See #3231, #3226, #3213, #3170, #3146, #3137, #2750, #2699, #2691, #1667, and many more besides.

@TropicalThunder
Copy link
Author

This looks similar to records. See #3231, #3226, #3213, #3170, #3146, #3137, #2750, #2699, #2691, #1667, and many more besides.

The others propose several other large and more comprehensive changes whereas this is much more specific, and that ought to be looked at for the future.

@DavidArno
Copy link

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.

@TropicalThunder
Copy link
Author

So this is just a syntax variation on what is already in planning for v9.

That I did not know. Thanks! All I am saying is that something has to be done.

@Richiban
Copy link

This is not records but primary constructors, which also have their own proposal. #2691

@canton7
Copy link

canton7 commented Mar 20, 2020

@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.

@Richiban
Copy link

Richiban commented Mar 20, 2020

@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.

@canton7
Copy link

canton7 commented Mar 20, 2020

@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.

@yulObraz
Copy link

yulObraz commented Apr 28, 2020

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.

@OskarKlintrot
Copy link

@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#.

@Richiban
Copy link

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)

@Richiban
Copy link

Primary constructors will allow something similar to this:

public class C(ServiceA a, ServiceB b)
{
	public void M()
	{
		a.DoSomething();
		b.DoSomethingElse();
	}
}

@YairHalberstadt
Copy link
Contributor

Closing, as this is being considered as part of the discussion surrounding primary constructors: #2691

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants