Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut to instantiate an object in Visual Studio

I have a class with more than 8 properties, and I often want to instantiate it in some method, but it is super tedious to have to write the properties one by one to assign them the value.

Is there any way to insert a kind of "code fragment" with a keyboard shortcut, which allows me to insert the instance of the class, and I just modify the values ​​to add?.

I do not want to use constructors because I want the instance to be readable for the reader of my code and because the constructors do not work in LinQ to SQL.

I Use Visual Studio 2015, C#, Resharper.

Thank you very much.

like image 323
HenryGuillen17 Avatar asked Jan 26 '18 15:01

HenryGuillen17


2 Answers

If you are initialising like this:

  var example = new Example()
    {
        ...
    };

Then you can use Ctrl-Space and it will keep offering you properties left for auto complete that you have yet to set.

like image 130
Ben Hall Avatar answered Nov 02 '22 16:11

Ben Hall


I've created the Generate an initializer for a new object with names of public properties and fields command for the Visual Commander extension. Call it after entering the class name and it will generate an initializer:

enter image description here

like image 37
Sergey Vlasov Avatar answered Nov 02 '22 16:11

Sergey Vlasov