13

Is there any way to automatically create the constructor for a class based on the properties in the class like Eclipse does? (Without getting ReSharper). I'm using Visual Studio 2008 (C#).

If this is a duplicate, please link (I tried searching).

Awaken
  • 1,243
  • 2
  • 14
  • 26
  • 2
    http://stackoverflow.com/questions/2976363/how-do-i-generate-a-constructor-from-class-fields-using-visual-studio-and-or-re – Mauricio Scheffer Sep 22 '12 at 02:35
  • Possible duplicate of [How do I generate a constructor from class fields using Visual Studio (and/or ReSharper)?](https://stackoverflow.com/questions/2976363/how-do-i-generate-a-constructor-from-class-fields-using-visual-studio-and-or-re) – Pouya Samie Oct 09 '17 at 18:36

4 Answers4

14

I answered the question here :

this is my answer :

In visual studio 2015 Update3, I have this feature.

just by Highlighting properties and then press ctrl + . and then Press Generate Constructor.

UPDATE For example, if you've highlighted 2 properties it will suggest you create a contractor with 2 parameters and if you've selected 3 it will suggest with 3 parameters and so on.

also works with VS2017.

enter image description here

Pouya Samie
  • 3,718
  • 1
  • 21
  • 34
9

you can use object initializer instead of having created a constructor, if you're using C# 3.0.

Referring code that I found in some example.

   class Program
   {
      public class Student
      {
         public string firstName;
         public string lastName;
      }
      public class ScienceClass
      {
         public Student Student1, Student2, Student3;
      }
      static void Main(string[] args)
      {
         var student1 = new Student{firstName = "Bruce",
                                    lastName  = "Willis"};
         var student2 = new Student{firstName = "George",
                                    lastName  = "Clooney"};
         var student3 = new Student{firstName = "James",
                                    lastName  = "Cameron"};
         var sClass = new ScienceClass{Student1 = student1,
                                       Student2 = student2,
                                       Student3 = student3};
      }
   }
this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137
  • 1
    The problem with this approach is you can't use it with `readonly` fields (or properties backed with `readonly` fields). – Dai May 23 '16 at 01:45
7

No. There is the ctor snippet(not quite what you were looking for), or you can create your macro. Possibly check out Productivity macros for C#. And since you don't like ReSharper, you can use CodeRush.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • Just for clarity, I am not anti-ReSharper. I just can't get it on this machine now, so I was hoping there was a built in way. That CodeRush example is exactly what I was thinking. – Awaken Jul 22 '10 at 18:17
  • The link on the answer is dead, is there any way that you can refresh it maybe? – Tolga Evcimen Dec 03 '15 at 07:51
2

Here's a pretty good work around:

  1. Make an empty class

    class MyClass{
    
    }
    
  2. Try to create an instance of the object and parse it the variable types you would like in the constructor

    class Program{
        static void Main(string[] args){
    
            string var1 = "First variable is a string";
            int var2 = "Second variable is an int";
    
            Myclass foo = new MyClass(var1, var2);
            //this line here is the important one
        }
    }
    
  3. Visual Studio should give you a resolve prompt if you mouse over the new MyClass which will allow an automatic creation of the constructor and properties in the class with whatever variable names you offered in step 2. Result is as follows.

    class MyClass{
        private string var1;
        private int var2;
    
        public MyClass(string var1, int var2){
            // TODO: Complete member initialization
            this.var1 = var1;
            this.var2 = var2;
        }
    }
    

    Note: You could even skip step 1 and use resolve twice to firstly generate the class, then generate the innards.

Josh Dean
  • 1,593
  • 2
  • 11
  • 17
  • The variable initilization of var1 and var2 are also important for variable type (string and int) – Josh Dean Jun 24 '13 at 07:37
  • The best way to use this for property initialization is from the class itself after the properties has been declared, then mocking a call to a constructor using those properties will be initialized – MatanCo Mar 02 '16 at 21:11