Reputation: 12990
Can VS 2012 professional generate a class constructor without any addons?
I can't believe I can't find the option to do this, and if it can't do it, it truly is a conspiracy :)
I have my class defined:
public class User
{
public int Id {get;set;}
public string Name {get;set;}
}
Now is there a shortcut that will generate the constructor, toString()
method etc?
Upvotes: 0
Views: 338
Reputation: 9690
"ctor" snippet generates a constructor, but without parameters. To get them, you should unfortunately use (free or not) addons. This question has been already discussed, see Shortcut for creating constructor with variables (C# VS2010) for example.
I think there is no direct mean to generate a paramerized constructor in a bare Visual Studio.
Edit : I should probably have added after my last sentence "... which strictly applies to properties and fields currently defined in class". But yes, following Peter's advice it is possible to generate a kind of parameterized constructor.
Upvotes: -1
Reputation: 223332
If you need a default constructor then there is a code snippet ctor
for it.
But if you need a constructor with parameters then in you code write:
User user = new User(2, "Name");
This will be an error, since there is no constructor with two parameters, but you will get a blue under line if you hover your mouse over User
in new User
. Click on that or put your cursor on User
and press Ctrl + .
It will give you an option to generate constructor like:
That will give you a constructor with fields like:
public class User
{
private int p1;
private string p2;
public User(int p1, string p2)
{
// TODO: Complete member initialization
this.p1 = p1;
this.p2 = p2;
}
public int Id { get; set; }
public string Name { get; set; }
}
Then you have to go in and remove p1
, p2
and point to Id
, Name
and also rename parameters in constructor. That is probably the best you can do with only Visual studio.
See: Generate From Usage - MSDN (thanks to @Peter Ritchie)
Consider installing Re-Sharper it has much better option for generating not only constructor but other very helpful code.
Upvotes: 3
Reputation: 1677
snippets are built in and you can add your own. ctor will make the constructor, I don't think there is anything for ToString, though you can just type override, pick it from the list and it will stub it out.
Upvotes: 0