I'm a little new to C# still... I'm finding myself reusing a specific procedure over and over. Before I go writing a helper method for personal laziness, is there a shorter or less wrong way to write this sort of statement?
Dictionary<string, string> data = someBigDictionary;
string createdBy;
data.TryGetValue("CreatedBy", out createdBy);
//do that for 15 other values
...
MyEntity me = new MyEntity{
CreatedBy = createdBy ?? "Unknown",
//set 15 other values
...
}
Essentially, set a property on an object by trying to get a value, then if it's null use a default value. I have a LOT of properties, it would be better if I could just
MyEntity me = new MyEntity{
CreatedBy = TryToGetValueOrReturnNull(data, "CreatedBy") ?? "Unknown",
...
}
Again, I am perfectly capable of writing my own helper function. I'm looking for an existing native functionality or shorthand before I do so.