Allow object initializer syntax outside of initialization #4648
Unanswered
Ekl1p5e
asked this question in
Language Ideas
Replies: 1 comment 1 reply
-
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am working with some code that initializes the same object in different places, setting different properties. It would be nice to call a method that sets certain properties and maybe chain them together to eliminate duplicate code. Here is a simple example of how it would work:
class Person
{
string FirstName {get; set;}
string LastName {get; set;}
string MiddleName {get; set;}
int Age {get; set;}
}
Person GetPerson(string firstName, string lastName)
{
return new Person
{
FirstName = firstName,
LastName = lastName,
};
}
Person GetPerson(string firstName, string lastName, int age){
return GetPerson(firstName, lastName){
Age = age,
}
}
I think this would neater in places where multiple properties need to be set on an object as well.
Beta Was this translation helpful? Give feedback.
All reactions