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 you initialize automatic list properties?

How do you initialize automatic list properties?

Scheduled Pinned Locked Moved C#
question
9 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.
  • T Offline
    T Offline
    T M Gray
    wrote on last edited by
    #1

    publc class Thing
    {
    Thing () {}

    public List<Foo> MyFoos {get; set;}
    }

    How do you get the list to be defaulted to an empty list rather than null? Do I need to implement a private backing variable?

    N L 2 Replies Last reply
    0
    • T T M Gray

      publc class Thing
      {
      Thing () {}

      public List<Foo> MyFoos {get; set;}
      }

      How do you get the list to be defaulted to an empty list rather than null? Do I need to implement a private backing variable?

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Either use a backing field and lazy initialization

      public class Thing
      {
      private List m_List;

      Thing() { }

      public List MyFoos
      {
      get
      {
      if(m_List == null)
      m_List = new List();

         return m\_List;
       }
       set{ m\_List = value; }
      

      }

      or use the constructor

      public class Thing
      {
      Thing()
      {
      MyFoos = new List();
      }

      public List MyFoos {get; set;}
      }


      I know the language. I've read a book. - _Madmatt

      F T 2 Replies Last reply
      0
      • T T M Gray

        publc class Thing
        {
        Thing () {}

        public List<Foo> MyFoos {get; set;}
        }

        How do you get the list to be defaulted to an empty list rather than null? Do I need to implement a private backing variable?

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

        Or initialize it in the constructor :) --edit What Mark said!

        I are Troll :suss:

        1 Reply Last reply
        0
        • N Not Active

          Either use a backing field and lazy initialization

          public class Thing
          {
          private List m_List;

          Thing() { }

          public List MyFoos
          {
          get
          {
          if(m_List == null)
          m_List = new List();

             return m\_List;
           }
           set{ m\_List = value; }
          

          }

          or use the constructor

          public class Thing
          {
          Thing()
          {
          MyFoos = new List();
          }

          public List MyFoos {get; set;}
          }


          I know the language. I've read a book. - _Madmatt

          F Offline
          F Offline
          fjdiewornncalwe
          wrote on last edited by
          #4

          Excellent answer... (Because I do it this way, of course.)

          I wasn't, now I am, then I won't be anymore.

          1 Reply Last reply
          0
          • N Not Active

            Either use a backing field and lazy initialization

            public class Thing
            {
            private List m_List;

            Thing() { }

            public List MyFoos
            {
            get
            {
            if(m_List == null)
            m_List = new List();

               return m\_List;
             }
             set{ m\_List = value; }
            

            }

            or use the constructor

            public class Thing
            {
            Thing()
            {
            MyFoos = new List();
            }

            public List MyFoos {get; set;}
            }


            I know the language. I've read a book. - _Madmatt

            T Offline
            T Offline
            T M Gray
            wrote on last edited by
            #5

            Do you have to do it in every constructor? I put it in the default constructor but it doesn't seem to work when I instatiate using a different constructor.

            F 1 Reply Last reply
            0
            • T T M Gray

              Do you have to do it in every constructor? I put it in the default constructor but it doesn't seem to work when I instatiate using a different constructor.

              F Offline
              F Offline
              fjdiewornncalwe
              wrote on last edited by
              #6

              You need to append :basename() to the constructor definitions.

              public class MySuperAwesomeClass
              {
              public int AwesomeOne { get; set; }
              public int NotAsAwesome { get; set; }
              public string UselessCrap { get; set; }

              public MySuperAwesomeClass()
              {
                  AwesomeOne = 42;
                  NotAsAwesome = -1;
              }
              
              public MySuperAwesomeClass( string someValue ) : base()
              {
                  UselessCrap = someValue;
              }
              

              }

              I wasn't, now I am, then I won't be anymore.

              T P 2 Replies Last reply
              0
              • F fjdiewornncalwe

                You need to append :basename() to the constructor definitions.

                public class MySuperAwesomeClass
                {
                public int AwesomeOne { get; set; }
                public int NotAsAwesome { get; set; }
                public string UselessCrap { get; set; }

                public MySuperAwesomeClass()
                {
                    AwesomeOne = 42;
                    NotAsAwesome = -1;
                }
                
                public MySuperAwesomeClass( string someValue ) : base()
                {
                    UselessCrap = someValue;
                }
                

                }

                I wasn't, now I am, then I won't be anymore.

                T Offline
                T Offline
                T M Gray
                wrote on last edited by
                #7

                Appending :base() doesn't run the default constructor for that class. The debugger never enters the default constructor and all my list properties are still null if I instantiate using a different constructor.

                P 1 Reply Last reply
                0
                • T T M Gray

                  Appending :base() doesn't run the default constructor for that class. The debugger never enters the default constructor and all my list properties are still null if I instantiate using a different constructor.

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

                  Rather than append :base(), you should use :this() to call the default constructor in the current class. The use of base implies a call to a base class, whereas this refers to the current class.

                  I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Onyx

                  1 Reply Last reply
                  0
                  • F fjdiewornncalwe

                    You need to append :basename() to the constructor definitions.

                    public class MySuperAwesomeClass
                    {
                    public int AwesomeOne { get; set; }
                    public int NotAsAwesome { get; set; }
                    public string UselessCrap { get; set; }

                    public MySuperAwesomeClass()
                    {
                        AwesomeOne = 42;
                        NotAsAwesome = -1;
                    }
                    
                    public MySuperAwesomeClass( string someValue ) : base()
                    {
                        UselessCrap = someValue;
                    }
                    

                    }

                    I wasn't, now I am, then I won't be anymore.

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

                    Not quite. I suspect you're thinking of :this() instead.

                    I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Onyx

                    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