Replies: 1 comment
-
I do miss with blocks from VB. They are about the only thing I miss from VB. I would like to have something like that in C#, I just do not like the syntax you propose:
I do not like the =, since you are not assigning customer, I do not like (), since you are not invoking a constructor... Here is one example of a 'with block-like' construct coming in handy. What I regularly encounter, is when I need to refactor a 'new' statement with an object initializer:
When I want to delegate instantiation to a method or factory, it would be cool if the code would remain mostly the same:
Currently I end up having to click and type a lot, and the code needs to be so much different depending on such a trivial fact:
It gets worse with longer examples, ofcourse. When object initializers were nested, I need to totally flatten out the nesting. I suspect that this may provoke comment about syntax ambiguity I am overlooking, but I would propose that behind method calls, property invokations, field references, etc. you could put an object initializer, just like after a new statement. |
Beta Was this translation helpful? Give feedback.
-
@dg2k commented on Tue May 24 2016
Synopsis
Setting properties on object initialization makes the code readable with a single statement.
If I want to reuse the
customer
reference by reassigning properties, unless I am missing something, there is no compact form that is similar to initializing a new object. Each property setting requires a statement which means as many code lines.Proposal
As a proposal, consider a keyword
assign
in place ofnew
, not for creating an object, rather to assign property values. That is,assign
is simply a syntactic equivalent ofnew
initialization, but for assigning property values which otherwise requires more lines.or
Discussion
The above is just a convenience for re-assigning of property values to an existing object. The second version above even drops out the Type
Customer
as it should be obvious from the reference.There are numerous usage scenarios where this can be convenient. To ready my reference for next use, for instance, I can assign default property values as:
The assignment with object initialization syntactic equivalent makes the code more readable, particularly where a number of properties is large. There are of course caveats -- if the reference is not already initialized, the assignment will not work as expected. But then again, assign (or something equivalent) can act as new in this situation, in which case it acts to re-assign if the reference is already initialized, else create a new object. Makes sense?
Apologies in advance if an equivalent of this exists already but haven't come across yet.
@s-arash commented on Tue May 24 2016
I propose this syntax:
With customer
.OrderId=0
.UserId=0
End With
@Unknown6656 commented on Wed May 25 2016
Your proposal looks like a C# -equivalent of the VB-keyword
with
.I am pretty sure, that this proposal has been made about 100 times before and I will reference the according issues when I found them ;)
EDIT:
This isssue is/may be a duplicate of:
dotnet/roslyn#6553
dotnet/roslyn#7322
dotnet/roslyn#5172 (comment)
dotnet/roslyn#5172 (comment)
dotnet/roslyn#5676
dotnet/roslyn#5445 (partly mentioned)
I can list more similar issues, when I return from University this evening :)
@dg2k commented on Wed May 25 2016
Thank you so much @Unknown6656 for taking the time to dig into similar past proposals. I've never assumed no-one thought of such a basic need in C# programming and the last sentence of my proposal confirms my very suspicion for the existence of similar ideas.
But at a different level this confirms an important point -- why are proposals widely supported like this one not taken up? Duplication of the same idea are like votes for the same thing. I wish the search system is a bit better, with flagging of similar ideas upon editing (like SO question posting), in which case I could have avoided this duplication.
@Unknown6656 commented on Wed May 25 2016
@dg2k I know exactly, what you mean, I also have the need for the proposal you made ;)
Regarding your point about GitHub's search system: GitHub is primarily based around source control and team project control, less around issue search etc. (I also absolutely love how SO implemented the posting system)
Regarding your point about why C# does not (yet) have this feature implemented: The developers focus was probably primarily concentrated on performance and other language aspects....
At least, the isssue dotnet/roslyn#5172 is currently marked as
3 - Working
, so that is something, I guess....@dg2k commented on Thu May 26 2016
@Unknown6656 Thanks again for the update and I'm now more in tune with this particular feature request.
I understand GitHub's particular strength vis-à-vis SO -- if there is such thing (or is there?) as the Oscar of online productivity tools (or whatever more suitable phrase you can attach here), GitHub should get it. Of course, in my opinion ;-)
Beta Was this translation helpful? Give feedback.
All reactions