Proposal: Create objects easily #1499
Replies: 9 comments
-
EmployeeListViewModel objEmpListModel = new();
EmployeeListViewModel objEmpListModel = new(empId: intEmpId, empName: strEmpName); |
Beta Was this translation helpful? Give feedback.
-
I think HaloFour has a better solution. |
Beta Was this translation helpful? Give feedback.
-
var objEmpListModel = new EmployeeListViewModel(); |
Beta Was this translation helpful? Give feedback.
-
With all due respect to your suggestions, this goes hand in hand with As you simply instantiate with |
Beta Was this translation helpful? Give feedback.
-
That syntax doesn't exist in C#. In C++ that syntax carries additional implications as to where and how the class is being constructed which does not apply in C#. The proposal I linked previously is already championed and under development. It's unlikely that the team would consider two proposals to accomplish the same thing. Target-typed new expressions can be used in more scenarios, such as method parameters, with even less verbosity. |
Beta Was this translation helpful? Give feedback.
-
@theswanand please be careful with using The current way is actually symmetric if one understands the value/reference type difference.
|
Beta Was this translation helpful? Give feedback.
-
If there's one thing I don't want to lose when we get a shorthand for this, it's the |
Beta Was this translation helpful? Give feedback.
-
This is not strictly true, actually. I recall that the discussion in #99 notes that there can already by struct types out there where |
Beta Was this translation helpful? Give feedback.
-
I am guessing you are coming from a C++ background. In C++, allocing with new and allocing with "name()" has different effects, hence why they are created differently (in hindsight, the C++ standard could probably use a dedicated keyword for stack alloc). This causes problems when you have code such as this
Although you can work around this limitation in your own code, this makes it harder for library designers when they name their objects. On top of it, having a clear keyword for memory allocation makes said action easier to detect. Memory allocations would otherwise go undetected in places where performance is critical. I really don't think C# needs a feature like this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a cosmetic proposal for creating objects by omitting the
new
keyword.Scenario 1: Creating object with default constructor
Current:
EmployeeListViewModel objEmpListModel = new EmployeeListViewModel(); //Type obj = new Type();
Proposed:
EmployeeListViewModel objEmpListModel(); //Type obj();
Scenario 2: Creating object with other constructors
Proposed:
EmployeeListViewModel objEmpListModel(intEmpId, strEmpName);
EmployeeListViewModel objEmpListModel(empId: intEmpId, empName: strEmpName);
EmployeeListViewModel objEmpListModel{empId= intEmpId, empName= strEmpName};
So we can use just two keywords to both declare a reference type and then instantiate it.
Advantage:
Type
name has to be written once. This is really helpful when dealing with classes having big names and occupies two lines while passing arguments to the constructor.NOTE:
This proposal will be helpful only when object and reference
Type
are same.Beta Was this translation helpful? Give feedback.
All reactions