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. What this mean ??. This code works fine

What this mean ??. This code works fine

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.
  • A Offline
    A Offline
    AghaKhan
    wrote on last edited by
    #1

    public class Item { public double X { get; set; } public double Y { get; set; } public string Name { get; set; } public string Color { get; set; } } public class ItemsFactory { private List items; public IEnumerable Items { get { return items ?? (items = new List<Item>() { new Item { Name = "One", X = 33, Y = 25, Color = "Red" }, new new Item { Name = "Two", X = 44, Y = 99, Color"Blue" } }); } } } :doh:

    L P 2 Replies Last reply
    0
    • A AghaKhan

      public class Item { public double X { get; set; } public double Y { get; set; } public string Name { get; set; } public string Color { get; set; } } public class ItemsFactory { private List items; public IEnumerable Items { get { return items ?? (items = new List<Item>() { new Item { Name = "One", X = 33, Y = 25, Color = "Red" }, new new Item { Name = "Two", X = 44, Y = 99, Color"Blue" } }); } } } :doh:

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      MSDN wrote:

      The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

      :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      A 1 Reply Last reply
      0
      • A AghaKhan

        public class Item { public double X { get; set; } public double Y { get; set; } public string Name { get; set; } public string Color { get; set; } } public class ItemsFactory { private List items; public IEnumerable Items { get { return items ?? (items = new List<Item>() { new Item { Name = "One", X = 33, Y = 25, Color = "Red" }, new new Item { Name = "Two", X = 44, Y = 99, Color"Blue" } }); } } } :doh:

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

        The ?? is known as the null coalescing operator. That's a fancy dance way of saying that it tests to see if the left hand side is null, and if it is, it uses the right hand side. What this is doing, in your example, is evaluate items to see if it's null, and if it is, it assigns a new list to it and returns that. In practical terms, this is the same as doing this:

        if (items == null)
        items = new List<Item>();
        return items

        I have missed the addition of the Item instances to the array to simplify this example, but you should get the idea from it. [Edit]The OP deleted the original message. It was: public class Item { public double X { get; set; } public double Y { get; set; } public string Name { get; set; } public string Color { get; set; } } public class ItemsFactory { private List items; public IEnumerable Items { get { return items ?? (items = new List() { new Item { Name = "One", X = 33, Y = 25, Color = "Red" }, new new Item { Name = "Two", X = 44, Y = 99, Color"Blue" } }); } } }

        Forgive your enemies - it messes with their heads

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        L 1 Reply Last reply
        0
        • P Pete OHanlon

          The ?? is known as the null coalescing operator. That's a fancy dance way of saying that it tests to see if the left hand side is null, and if it is, it uses the right hand side. What this is doing, in your example, is evaluate items to see if it's null, and if it is, it assigns a new list to it and returns that. In practical terms, this is the same as doing this:

          if (items == null)
          items = new List<Item>();
          return items

          I have missed the addition of the Item instances to the array to simplify this example, but you should get the idea from it. [Edit]The OP deleted the original message. It was: public class Item { public double X { get; set; } public double Y { get; set; } public string Name { get; set; } public string Color { get; set; } } public class ItemsFactory { private List items; public IEnumerable Items { get { return items ?? (items = new List() { new Item { Name = "One", X = 33, Y = 25, Color = "Red" }, new new Item { Name = "Two", X = 44, Y = 99, Color"Blue" } }); } } }

          Forgive your enemies - it messes with their heads

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

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

          You put it much better than MSDN! :)

          It’s not because things are difficult that we do not dare, it’s because we do not dare that things are difficult. ~Seneca

          P 1 Reply Last reply
          0
          • L Lost User

            You put it much better than MSDN! :)

            It’s not because things are difficult that we do not dare, it’s because we do not dare that things are difficult. ~Seneca

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

            Thanks Annie. That's because, unlike MSDN, I'm trying to educate. :-D

            Forgive your enemies - it messes with their heads

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            A 1 Reply Last reply
            0
            • L Luc Pattyn

              MSDN wrote:

              The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

              :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              A Offline
              A Offline
              AghaKhan
              wrote on last edited by
              #6

              Thanks. :laugh:

              1 Reply Last reply
              0
              • P Pete OHanlon

                Thanks Annie. That's because, unlike MSDN, I'm trying to educate. :-D

                Forgive your enemies - it messes with their heads

                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                A Offline
                A Offline
                AghaKhan
                wrote on last edited by
                #7

                Thanks :laugh:

                S 1 Reply Last reply
                0
                • A AghaKhan

                  Thanks :laugh:

                  S Offline
                  S Offline
                  Smithers Jones
                  wrote on last edited by
                  #8

                  Why did you delete your question? Don't do that.

                  "I love deadlines. I like the whooshing sound they make as they fly by." (DNA)

                  P 1 Reply Last reply
                  0
                  • S Smithers Jones

                    Why did you delete your question? Don't do that.

                    "I love deadlines. I like the whooshing sound they make as they fly by." (DNA)

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

                    I have edited my answer to show what the OP asked.

                    Forgive your enemies - it messes with their heads

                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                    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