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. Constructor of Structs.

Constructor of Structs.

Scheduled Pinned Locked Moved C#
question
16 Posts 4 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.
  • M mmikey7

    Limitation is that structs cannot contain explicit parameterless constructors. But you certainly can use Console.WriteLine in sturct's constructor. See this example: using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Test t = new Test(7); Console.ReadLine(); } } public struct Test { public Test(int x) { Console.WriteLine("I'm Test's constructor and this is my x parameter: "+ x.ToString()); } } }

    S Offline
    S Offline
    SPanicker
    wrote on last edited by
    #7

    Yes it is possible only if the struct is instantiated (as if it was an object). Not in the other case when we declare it without the new operator(as if it was a value type variable). Any suggestions?

    Regards, Lenus.

    S M 2 Replies Last reply
    0
    • S SPanicker

      Yes it is possible only if the struct is instantiated (as if it was an object). Not in the other case when we declare it without the new operator(as if it was a value type variable). Any suggestions?

      Regards, Lenus.

      S Offline
      S Offline
      SPanicker
      wrote on last edited by
      #8

      It seems as if the constructors don't perform any other work than to assign fields withe values when created as a value type(without new operator).

      Regards, Lenus.

      1 Reply Last reply
      0
      • S SPanicker

        Yes it is possible only if the struct is instantiated (as if it was an object). Not in the other case when we declare it without the new operator(as if it was a value type variable). Any suggestions?

        Regards, Lenus.

        M Offline
        M Offline
        mmikey7
        wrote on last edited by
        #9

        SPanicker* wrote:

        Not in the other case when we declare it without the new operator(as if it was a value type variable).

        Now I know what you were asking, it was not clear from your initial post. In that case, default parameterless constructor is called and as I said before you cannot declare or override paremeterless constructor.

        S 1 Reply Last reply
        0
        • S SPanicker

          No I just want to print out something from the struct's constructor which is not happening. Why is it so?

          Regards, Lenus.

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

          Go forth and try it.

          S 1 Reply Last reply
          0
          • M mmikey7

            SPanicker* wrote:

            Not in the other case when we declare it without the new operator(as if it was a value type variable).

            Now I know what you were asking, it was not clear from your initial post. In that case, default parameterless constructor is called and as I said before you cannot declare or override paremeterless constructor.

            S Offline
            S Offline
            SPanicker
            wrote on last edited by
            #11

            Even in this case, the constructor(which i provide) is getting read and the values are assigned. Only exception is that,I cannot call any other functions from inside that( But values can be assigned to variables ). I still need two points to be cleared: As you said if the parameterless constructor cannot be overridden then how is this happening? If the parameterless constructor cannot be overriden, then why is C# designed to accept such constructors which are of no use? Thanks.

            Regards, Lenus.

            M 1 Reply Last reply
            0
            • P PIEBALDconsult

              Go forth and try it.

              S Offline
              S Offline
              SPanicker
              wrote on last edited by
              #12

              I tried. Its not possible from the constructor I create for a value type (declared without new keyword) struct.

              Regards, Lenus.

              1 Reply Last reply
              0
              • S SPanicker

                Even in this case, the constructor(which i provide) is getting read and the values are assigned. Only exception is that,I cannot call any other functions from inside that( But values can be assigned to variables ). I still need two points to be cleared: As you said if the parameterless constructor cannot be overridden then how is this happening? If the parameterless constructor cannot be overriden, then why is C# designed to accept such constructors which are of no use? Thanks.

                Regards, Lenus.

                M Offline
                M Offline
                mmikey7
                wrote on last edited by
                #13

                You declare(override) parameterless constructor? I'm lost here, can you past piece of code with your struct?

                S 1 Reply Last reply
                0
                • M mmikey7

                  You declare(override) parameterless constructor? I'm lost here, can you past piece of code with your struct?

                  S Offline
                  S Offline
                  SPanicker
                  wrote on last edited by
                  #14

                  Hi Michal, Sorry what i meant was, I have a constructor for a (value type - declared without new keyword) struct. This constructor cannot invoke any inbuilt functions like Console.WriteLine or user defined ones. But at the same time it can do assign values for fields. This seems quite strange. But, the same things works well(from inside the constructor)if it is declared with the new keyword. Pls. chk this sample code and tell me if somethngs wrong: using System; struct newStruct { //case 1: With struct new operator public newStruct(int a) { Console.WriteLine("In newStruct"); Console.WriteLine("{0}",a); } } struct anotherStruct { //case : Without new operator - like a value type variable. public int x; public anotherStruct(int a) { Console.WriteLine("In newStruct"); x = a; Console.WriteLine("{0},{1}",a,x); } public void method1() { Console.WriteLine(x); } } class AppEntry { public static void Main() { newStruct abc = new newStruct(10); anotherStruct def; def.x= 250; def.method1(); } }

                  Regards, Lenus.

                  M 1 Reply Last reply
                  0
                  • S SPanicker

                    Hi Michal, Sorry what i meant was, I have a constructor for a (value type - declared without new keyword) struct. This constructor cannot invoke any inbuilt functions like Console.WriteLine or user defined ones. But at the same time it can do assign values for fields. This seems quite strange. But, the same things works well(from inside the constructor)if it is declared with the new keyword. Pls. chk this sample code and tell me if somethngs wrong: using System; struct newStruct { //case 1: With struct new operator public newStruct(int a) { Console.WriteLine("In newStruct"); Console.WriteLine("{0}",a); } } struct anotherStruct { //case : Without new operator - like a value type variable. public int x; public anotherStruct(int a) { Console.WriteLine("In newStruct"); x = a; Console.WriteLine("{0},{1}",a,x); } public void method1() { Console.WriteLine(x); } } class AppEntry { public static void Main() { newStruct abc = new newStruct(10); anotherStruct def; def.x= 250; def.method1(); } }

                    Regards, Lenus.

                    M Offline
                    M Offline
                    mmikey7
                    wrote on last edited by
                    #15

                    Hi Lenus, there is nothing wrong with your code. In struct newStruct you defined constructor with parameter that is called when you create instance of newStruct: newStruct abc = new newStruct(10); In struct anotherStruct you also defined constructor with parameter, but this constructor is never called. anotherStruct def; //here you create object def of type anotherStruct. no costructor is called def.x = 250; // here you just assign value 250 to variable x, no constructor is called def.method1(); //this writes 250 to output which is totally correct since you set x to 250 before Compiler won't compile code if you do not set def.x. I was probably wrong when I said there is default parameterless constructor. There is no default parameterless constructor at all and you can not declare your own.

                    S 1 Reply Last reply
                    0
                    • M mmikey7

                      Hi Lenus, there is nothing wrong with your code. In struct newStruct you defined constructor with parameter that is called when you create instance of newStruct: newStruct abc = new newStruct(10); In struct anotherStruct you also defined constructor with parameter, but this constructor is never called. anotherStruct def; //here you create object def of type anotherStruct. no costructor is called def.x = 250; // here you just assign value 250 to variable x, no constructor is called def.method1(); //this writes 250 to output which is totally correct since you set x to 250 before Compiler won't compile code if you do not set def.x. I was probably wrong when I said there is default parameterless constructor. There is no default parameterless constructor at all and you can not declare your own.

                      S Offline
                      S Offline
                      SPanicker
                      wrote on last edited by
                      #16

                      Ok. Thanks for the clarification. But I think this proves there is nothing wrong declaring a value type stuct with a constructor - which obviously of no use and does nothing.

                      Regards, Lenus.

                      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