-
Notifications
You must be signed in to change notification settings - Fork 4.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# feature idea: Using 'new' without the type name to call constructors #4413
Comments
It would be good idea |
this is nice for unwieldy long generic declarations |
Also for method parameters;
|
Feels a little backwards given the usual direction that inference generally happens in C#. Having some form of shorthand for situations where you can't use |
@paulomorgado it can easily be abused in such scenarios. The fact that the method name is completely undescriptive makes that example even worse. There is no way I can tell what that My personal preference is to even include the argument name:
|
@dsaf, the cryptic example is intentional. If you don't know what The biggest problem is the understanding by developers of method overload resolution criteria. But for simple cases, it reduces ceremony. Take the example of
Would adding the type ( I know that syntax clashes with anonymous type instantiation. But there's not overload for that method taking a |
It most definitely would not be nicer, but this is just my opinion. I cannot tell whether it's |
I used to be like that - hate It's ASP.NET MVC is full of anonymous types used by convention, which is a lot worst, I think. |
I could see using this in some situations. Like everything else, context matters. Sometimes I use named parameters in method calls, sometimes I don't. And sometimes I could see using this in either a method call or especially in some object initializers. Nice idea @e-master. |
Thanks @TyreeJackson! It's really nothing extraordinary, just came up when I had to write a long initializer for a class member, and I got somewhat upset about the redundant constructor call. The one thing I like about this feature is that it is pretty intuitive - any C# developer would get used to it very quickly. I can see the point @dsaf is making - I wouldn't like to start working on someone's code if it's full of calls like
My take on it is that it would be a useful feature and not more harmful than |
No need to be patronizing. I use
It's not a matter of inference implementation. As a human, until you familiarize yourself with the API or use an IDE feature - it can be anything.
Yeah, but even F# doesn't do this sort of inference AFAIK, and they are succinct-crazy.
Wouldn't it be nice to see code on GitHub or StackOverflow and understand it without copy-pasting to IDE?
The original proposal is not bad. |
I had no intention of being patronizing! |
@paulomorgado Oh, ok :). Yeah, I have seen people on both sides of |
They may like to write it. They pretend they like to read it. There is a very fine line between terse and succinct and it's very easy to cross, especially when you aren't yet at the point where you are six months or more down the road and you have to maintain that code. Or someone else has to maintain that code. A program is read significantly more often than it will be written, the experience should be optimized for that. The programming community has been through this a few times already over the decades. For a short time programming languages became obsessed with shorthand, to the point that any keyword and identifier could be referenced with as few characters as required to prevent ambiguity. It leads to
Those same tools also eliminate the need to have to manually type out that class name. If you have a fully qualified and creatable type on the left side of an assignment, Intellisense defaults to that fully qualified and creatable type on the right side. All you have to hit is tab.
The language shouldn't be walking them into making those stylistic mistakes. New developers should accidentally gravitate towards the pit of success and away from the pit of failure driven by the structure and grammar of the programming language. And for the record, I love |
I think that written code plays a role of social interaction between the humans that work with it. But some people are bla-bla-bla, others are just huh. |
Another way this could be used: public Contact CreateContact(string name, string address)
{
return new
{
Name = name,
Address = address
};
}
public object CreateAnonymousContact(string name, string address)
{
return new
{
Name = name,
Address = address
};
} At least you see what is the exact return type from the method signature, no need to guess. |
@dsaf public object CreateAnonymousContact(string name, string address) be public dynamic CreateAnonymousContact(string name, string address) in your second example of that last post? |
Indeed, there are many places where it would be useful. // instead of writing
_importCustomerDataCommand = new DelegateCommand(ImportCustomerData);
// you would write
_importCustomerDataCommand = new (ImportCustomerData); or // instead of writing
Task loader = new Task(() => LoadSomething(someClosure));
// you would write
Task loader = new (() => LoadSomething(someClosure)); |
@e-master // instead of writing
_importCustomerDataCommand = new DelegateCommand(ImportCustomerData);
// you would write
_importCustomerDataCommand = new (ImportCustomerData);
// or implicitly
_importCustomerDataCommand = ImportCustomerData; Slippery slopes and all that. |
That same thought occurred to me too, but I don't think that treating it like an implicit operator would be a good idea. I think it would not be intuitive that it's a constructor invocation, and that's only a parameter of the constructor. I would rather have someone define explicitly an implicit operator (pun intended) if it's really needed. |
@e-master |
yeah, slippery slope... :) |
@TyreeJackson anonymous objects can be implicitly cast to both |
@dsaf |
Dup of #35. |
Taken from: https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/9216582-using-new-without-the-type-name-to-call-construc
Similarly how the
var
keyword makes it simpler to declare an object in C#, thenew
keyword could also imply the type/constructor to be used to initialize an object.For example:
The compiler would be able to match constructor overloads as well, so potentially you could do the following:
This feature might be useful in list initializers too, e.g.:
And contrary to
var
it could be used in class member initialization.The text was updated successfully, but these errors were encountered: