Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I declare diverse constructors in C#

How do I declare diverse constructors in C#

Scheduled Pinned Locked Moved C#
questioncsharpvisual-studiocom
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xarzu
    wrote on last edited by
    #1

    In C#, can I declare more than one constructors? If I do create a second constructor in order to pass variables to, how can I do this and, if so, how? Also, if this is allowed, why does Visual Studio show this as a fault? https://tinyurl.com/ydesvncq

    Richard DeemingR D B S 4 Replies Last reply
    0
    • X Xarzu

      In C#, can I declare more than one constructors? If I do create a second constructor in order to pass variables to, how can I do this and, if so, how? Also, if this is allowed, why does Visual Studio show this as a fault? https://tinyurl.com/ydesvncq

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      What's with the shortened URL at the bottom of your message, pointing to a Google User Content page? That makes me suspect your account has be hijacked by spammers. :suss:


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • X Xarzu

        In C#, can I declare more than one constructors? If I do create a second constructor in order to pass variables to, how can I do this and, if so, how? Also, if this is allowed, why does Visual Studio show this as a fault? https://tinyurl.com/ydesvncq

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        First, nobody in their right mind is going to click that link. Next, yes, you can declare multiple constructor methods. You just have to make sure they differ in the list of parameters they take.

        public class Something
        public Something()
        {
        }

        public Something(string arg)
        {
        }
        
        public Something(int value)
        {
        }
        
        public Something(string arg, int value)
        {
        }
        

        }

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        1 Reply Last reply
        0
        • X Xarzu

          In C#, can I declare more than one constructors? If I do create a second constructor in order to pass variables to, how can I do this and, if so, how? Also, if this is allowed, why does Visual Studio show this as a fault? https://tinyurl.com/ydesvncq

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          Keep in mind that with .NET 4.0, and later, you can use optional parameters that are assigned a default value in the constructor's parameters: [^] I suggest you review the use of named and positional parameters, and the change to their behavior implemented in .NET 7.2: [^] You can have multiple constructors, and chain them as shown in the example below to support a variable number of parameters. This code sample is written to illustrate the several ways you can handle parameters and their default values: it is not intended as an example of code I'd write for "production." For example, what would be the use of an instance of the 'Person class with only a value for 'Age ?

          public class Person
          {
          const string defaultData = "default";
          const int defaultAge = -1;

          public string FirstName { get; } = defaultData;
          public string LastName { get; } = defaultData;
          public string MiddleName { get; } = defaultData;
          
          public int Age { set; get;}
          
          public Person(int age = defaultAge)
          {
              Age = age;
          }
          
          public Person(int age, string fname, string lname, string mname = null) : this(age) // chaining example
          {
              FirstName = fname;
              LastName = lname;
              MiddleName = mname;
          }
          
          public override string ToString()
          {
              return $"{FirstName} {MiddleName}, {LastName} Age: {Age}";
          }
          

          }

          «... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12

          P 1 Reply Last reply
          0
          • B BillWoodruff

            Keep in mind that with .NET 4.0, and later, you can use optional parameters that are assigned a default value in the constructor's parameters: [^] I suggest you review the use of named and positional parameters, and the change to their behavior implemented in .NET 7.2: [^] You can have multiple constructors, and chain them as shown in the example below to support a variable number of parameters. This code sample is written to illustrate the several ways you can handle parameters and their default values: it is not intended as an example of code I'd write for "production." For example, what would be the use of an instance of the 'Person class with only a value for 'Age ?

            public class Person
            {
            const string defaultData = "default";
            const int defaultAge = -1;

            public string FirstName { get; } = defaultData;
            public string LastName { get; } = defaultData;
            public string MiddleName { get; } = defaultData;
            
            public int Age { set; get;}
            
            public Person(int age = defaultAge)
            {
                Age = age;
            }
            
            public Person(int age, string fname, string lname, string mname = null) : this(age) // chaining example
            {
                FirstName = fname;
                LastName = lname;
                MiddleName = mname;
            }
            
            public override string ToString()
            {
                return $"{FirstName} {MiddleName}, {LastName} Age: {Age}";
            }
            

            }

            «... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12

            P Offline
            P Offline
            Peter_in_2780
            wrote on last edited by
            #5

            BillWoodruff wrote:

            For example, what would be the use of an instance of the 'Person class with only a value for 'Age ?

            Because some of us old farts are useless for anything else?

            Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

            1 Reply Last reply
            0
            • X Xarzu

              In C#, can I declare more than one constructors? If I do create a second constructor in order to pass variables to, how can I do this and, if so, how? Also, if this is allowed, why does Visual Studio show this as a fault? https://tinyurl.com/ydesvncq

              S Offline
              S Offline
              suresh446
              wrote on last edited by
              #6

              YES, you can create more than one constructor in c# by passing different parameters based on your requirements. check this article constructors in c# it will help you to understand constructors easily.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups