0

I have a class with 200 properties. I create an instance of this class and I have to give value to all props. Is there any shortcut in Visual Studio 2017 what writes me all props with a default value like null or 0? Something like this:

var myClass = new Class1()
{
    prop1 = "";
    prop2 = null;
    ...
    prop200 = 0;
}
  • 2
    _"I have a class with 200 properties"_ Who is responsible for this mess? https://en.wikipedia.org/wiki/Single_responsibility_principle If those properties all mean similar things you should use a single collection to store them. – Tim Schmelter Jun 06 '18 at 12:51
  • 2
    All properties will have the default value if you create an instance. You don't need additional code. – Tim Schmelter Jun 06 '18 at 12:53
  • And when you really have to specify non default stuff, consider deserialization form a manageable file/string format. – bommelding Jun 06 '18 at 12:55
  • why so much properties? can't you use a collection to store them? If your properties where primitives, they will always have default values. (int will be 0, string will be "", bool will be false, and so on) – William Borgo Jun 06 '18 at 13:30
  • See https://stackoverflow.com/questions/48464576/shortcut-to-instantiate-an-object-in-visual-studio – Sergey Vlasov Jun 07 '18 at 01:58
  • Thank you Sergey! – Dávid Szűcs Jun 07 '18 at 13:26

1 Answers1

0

As long as all properties don't have any initial values, so are all similar to:

public object MyProperty { get; set; }

Then they should all be null by default when you create an instance of the class (as long as the class constructor doesn't do anything with them of course)

ataraxia
  • 995
  • 13
  • 31