I am creating a model for users and I want that property joined was set to Now(). Here’s my code:
[DefaultValue(DateTime.Now)]
public DateTime joined {get; set;}
I get error:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.
What am I doing wrong? And what’s the best way to do what I want?
DateTime.Nowis not a constant, but a property that’s computed at runtime, which is why you can’t do what you’re suggesting.You can do what you’re proposing with either:
Or: