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. Question about constructors of a class

Question about constructors of a class

Scheduled Pinned Locked Moved C#
questionhelp
7 Posts 5 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.
  • A Offline
    A Offline
    Arun kumar Gautam
    wrote on last edited by
    #1

    Class a { a obj=new a(); } in this case we are calling the default constructor but class a { public a(int i) { console.writeline(i); } a obj=new a(); //Gives error during compilation } In this case we can't call default constructor after creating a custom constructor. But why it happns??? Why we cant use default constructor in this case

    P P D L 4 Replies Last reply
    0
    • A Arun kumar Gautam

      Class a { a obj=new a(); } in this case we are calling the default constructor but class a { public a(int i) { console.writeline(i); } a obj=new a(); //Gives error during compilation } In this case we can't call default constructor after creating a custom constructor. But why it happns??? Why we cant use default constructor in this case

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Because it doesn't have one.

      A 1 Reply Last reply
      0
      • P PIEBALDconsult

        Because it doesn't have one.

        A Offline
        A Offline
        Arun kumar Gautam
        wrote on last edited by
        #3

        means?, can u describe it

        1 Reply Last reply
        0
        • A Arun kumar Gautam

          Class a { a obj=new a(); } in this case we are calling the default constructor but class a { public a(int i) { console.writeline(i); } a obj=new a(); //Gives error during compilation } In this case we can't call default constructor after creating a custom constructor. But why it happns??? Why we cant use default constructor in this case

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          In your first version of class a, you haven't specified a constructor. This means that a default constructor is created for you. As soon as you add a constructor, you lose the default constructor. This is exactly what should happen because you can't instantiate a class that doesn't have a constructor, and you don't want the compiler to give someone the ability to use a default constructor if your class relies on values that are passed in via the constructor.

          I was brought up to respect my elders. I don't respect many people nowadays.
          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            In your first version of class a, you haven't specified a constructor. This means that a default constructor is created for you. As soon as you add a constructor, you lose the default constructor. This is exactly what should happen because you can't instantiate a class that doesn't have a constructor, and you don't want the compiler to give someone the ability to use a default constructor if your class relies on values that are passed in via the constructor.

            I was brought up to respect my elders. I don't respect many people nowadays.
            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            A Offline
            A Offline
            Arun kumar Gautam
            wrote on last edited by
            #5

            thnks for this answer i appreciate it

            1 Reply Last reply
            0
            • A Arun kumar Gautam

              Class a { a obj=new a(); } in this case we are calling the default constructor but class a { public a(int i) { console.writeline(i); } a obj=new a(); //Gives error during compilation } In this case we can't call default constructor after creating a custom constructor. But why it happns??? Why we cant use default constructor in this case

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              Classes only have the constructors you create - if you don't create any at all then a parameterless constructor is created for you. Structs, being value types, are different and always have a default parameterless constructor with all the fields initialized to their default values (normally zero, or null if the field is a reference type).

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              1 Reply Last reply
              0
              • A Arun kumar Gautam

                Class a { a obj=new a(); } in this case we are calling the default constructor but class a { public a(int i) { console.writeline(i); } a obj=new a(); //Gives error during compilation } In this case we can't call default constructor after creating a custom constructor. But why it happns??? Why we cant use default constructor in this case

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Because the language specification says so.

                17.10.4 Default constructors
                If a class contains no instance constructor declarations, a default instance constructor is automatically
                provided. That default constructor simply invokes the parameterless constructor of the direct base class.

                Really it should be have said "if and only if", because that's what they mean. If you don't define a ctor, the default one exists. If you do define a ctor, the default one goes away. You can get it back if you want, because it's trivial:

                public TypeName() { }

                (it would be protected for abstract classes)

                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