I have a public class PersonModel
with public getters and setters.
The relevant properties are:
DateOfBirth
, aDateTime
,Age
anint
.
In the code below, I have used the shorthand curly brace shorthand notation to create an abridged version of several lines of output.<property name>
.
PersonModel output = new PersonModel
{
PersonId = id,
FirstName = GetRandomItem(firstNames),
LastName = GetRandomItem(lastNames),
IsAlive = GetRandomItem(aliveStatuses),
DateOfBirth = GetRandomDate(),
AccountBalance = ((decimal)rnd.Next(1, 1000000) / 100)
};
output.Age = GetAgeInYears(output.DateOfBirth);
However, I would have liked to have inlined Age within the curly braces too.
However inlining this code, underneath DateOfBirth:
Age = GetAgeInYears(DateOfBirth),
leads to:
Error CS0103 The name 'DateOfBirth' does not exist in the current context..
And, I can't use:
Age = GetAgeInYears(output.DateOfBirth),
because that leads to:
Error CS0165 Use of unassigned local variable 'output'...
Is there a way to reference DateOfBirth
within the curly braces when setting Age
in C#4?
Something akin to the this
keyword specific to curly brace initialisation (without introducing a new variable for DOB prior to constructor).