C# Method-reference of constructor
Asked Answered
B

1

7

In C# I can have references of methods and static methods. Can I also get the reference of a classes constructor?

In Java I can say Supplier<MyClass> createMyClass = MyClass::new (instead of the longer lambda syntax).
In C# I only know the notation Func<MyClass> createMyClass = () => MyClass(). But I think the Java way with the constructor reference is better readable.

And I don't want to make a static CreateMyClass function. I really want the constructor.

Blowtube answered 23/5, 2017 at 6:40 Comment(0)
C
13

No, there's no equivalent of method group conversions for constructors, or properties, indexers or operators.

It's an entirely reasonable idea, but it isn't in C# at the moment. It is, however, tracked as part of a feature request in the C# design repo - so you may want to subscribe to that issue.

Carvelbuilt answered 23/5, 2017 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.