0

Say you have a constructor in a newly created class:

public MyClass( string input1, float input2)
{
}

Is there a shortcut to produce code analogous to this?

public string Input1 {get; set;}
public float Input2 {get; set;}

public MyClass(string input1, float input2)
{
    Input1 = input1;
    Input2 = input2;
}

Many thanks.

2
  • 1
    Not yet. Wait until C# 6 comes out. Commented Aug 23, 2014 at 14:47
  • 2
    What's with the downvotes? Commented Aug 23, 2014 at 15:14

2 Answers 2

2

It is possible that visual studio helps you out abit.

It works when the constructtor doesnt exist yet.

Just type:

SomeClass a = new SomeClass(input1, input2);

It will be underlined red as the constructor doesnt exist yet. Then Rightclick on the not yet existing Constructor and Click

Generate => Construcotr

The result will look like this:

string input1;
float input2;

public SomeClass(string input1, float input2)
{
    // Some comment i dont remember
    this.input1 = input1;
    this.input2 = input2;
}

edit It is possible, that this feature only exists in Premium / Ultimate edition. Not sure on that one.

Sign up to request clarification or add additional context in comments.

1 Comment

Worked for me with VS Professional 2013
1

Considering the fields (they are properties really) are public in your example, you don't actually need a constructor. You can initialize them when you construct a new instance:

var x = new SomeClass { input1 = "value1", input2 = 12345 };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.