Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8461437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:52:48+00:00 2026-06-10T13:52:48+00:00

Very common scenario: I have a class with multiple instance variables and a constructor

  • 0

Very common scenario: I have a class with multiple instance variables and a constructor that accepts multiple parameters. Can I somehow bind one to the other? Assigning all parameters to the instance variables is very verbose and is one of the situations that could be (and should be) covered by the convention-over-configuration principle. My example code looks like this:

public class myClass
{    
    private object param1;
    private object param2;
    private object param3;
    private object param4;

    public myClass(object param1, object param2, object param3, object param4)
    {
        this.param1 = param1;
        this.param2 = param2;
        this.param3 = param3;
        this.param4 = param4;
    }
}

Is there an easy way to get rid of this and let C# do its magic automatically?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:52:49+00:00Added an answer on June 10, 2026 at 1:52 pm

    While there are many plugins that can do this kind of thing – it’s worth noting that if you VS2010+’s native ability to generate constructors from a model call when a matching constructor does not exist, you can use a simple keyboard shortcut to do the work for you.

    I.e. if I a stub class:

    class MyClass{
    
    }
    

    And then in a method somewhere I write something like this:

    object p1, p2, p3;
    //...  (get values for p1-3)
    var a = new MyClass(p1, p2, p3);
    

    When such a constructor does not exist, a little helper button appears. If you click on that, or press ALT+SHIFT+F10 (by default) you get a menu, one of the options on which is to generate a stub constructor, which will then change the MyClass code as follows:

    class MyClass
    {
        private object p1;
        private object p2;
        private object p3;
    
        public MyClass(object p1, object p2, object p3)
        {
            // TODO: Complete member initialization
            this.p1 = p1;
            this.p2 = p2;
            this.p3 = p3;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario that i suspect is very common, i've found various ideas
This is a very common scenario: displaying images in a ListView which have to
I have what I believe is a very common scenario: I clone a development
I have what I think is a very common scenario. I have an invoice
In the very common scenario where you have a textbox, and some kind of
I imagine a very common scenario is where an entire dynamic page can be
I have this scenario, which I think must be pretty common: class Parameter {
This is a very common thing in web applications. If I have a user
Very common scenario using Ajax is to parse the response and create the user
Ok, this is an very common scenario in web dev, with a very common

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.