//The object returned from here will be immutable public ExampleClass GetFromDatabase(DBConnection conn, int id) { //do database stuff here (ommitted from example) ExampleClassParams parameters = new ExampleClassParams() { ExampleProperty = 1, ExampleProperty2 = 2 }; //Danger here as parameters object is mutable return(new ExampleClass(parameters)); //Danger is now over ;) }
//Private constructor, prohibiting construction outside of this class private ExampleClass(ExampleClassParams parameters) { _exampleProperty = parameters.ExampleProperty; //and so on... }