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. Null array in a dictionary

Null array in a dictionary

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
11 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.
  • P Offline
    P Offline
    PozzaVecia
    wrote on last edited by
    #1

    I need to instatiate a dictionary accepting null as value, but I have a syntax problem:

    Dictionary d = new Dictionary()
    d.Add("a", new double[2]{100,102});
    d.Add("b", null);
    d.Add("v", new double[2] { 99, 101 });;

    Is it possible to do it?

    L OriginalGriffO J 3 Replies Last reply
    0
    • P PozzaVecia

      I need to instatiate a dictionary accepting null as value, but I have a syntax problem:

      Dictionary d = new Dictionary()
      d.Add("a", new double[2]{100,102});
      d.Add("b", null);
      d.Add("v", new double[2] { 99, 101 });;

      Is it possible to do it?

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

      What happens when you try?

      Use the best guess

      1 Reply Last reply
      0
      • P PozzaVecia

        I need to instatiate a dictionary accepting null as value, but I have a syntax problem:

        Dictionary d = new Dictionary()
        d.Add("a", new double[2]{100,102});
        d.Add("b", null);
        d.Add("v", new double[2] { 99, 101 });;

        Is it possible to do it?

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        You don't need the '?' at all - you need that to specify a value type that can hold a null value. Reference types automatically can (as this is the default value for a reference type variable) All arrays are reference types, regardless of whether they are array of reference or value types. So what you need to say is:

        Dictionary<string, double[]> d = new Dictionary<string, double[]>();
        d.Add("a", new double[2] { 100, 102 });
        d.Add("b", null);
        d.Add("v", new double[2] { 99, 101 });

        You would only need the '?' if you wanted an array of doubles that could hold null values, and then the syntax would be:

        double?[] da = new double?[10];

        To indicate that the individual elements of the array could contain nulls.

        The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        P 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          You don't need the '?' at all - you need that to specify a value type that can hold a null value. Reference types automatically can (as this is the default value for a reference type variable) All arrays are reference types, regardless of whether they are array of reference or value types. So what you need to say is:

          Dictionary<string, double[]> d = new Dictionary<string, double[]>();
          d.Add("a", new double[2] { 100, 102 });
          d.Add("b", null);
          d.Add("v", new double[2] { 99, 101 });

          You would only need the '?' if you wanted an array of doubles that could hold null values, and then the syntax would be:

          double?[] da = new double?[10];

          To indicate that the individual elements of the array could contain nulls.

          The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

          P Offline
          P Offline
          PozzaVecia
          wrote on last edited by
          #4

          Thanks in this way I'm not able to find the Max value of first element

          var ks = d.Max(v => v.Value[0]);

          I didn't clarify before this port sorry

          OriginalGriffO 1 Reply Last reply
          0
          • P PozzaVecia

            Thanks in this way I'm not able to find the Max value of first element

            var ks = d.Max(v => v.Value[0]);

            I didn't clarify before this port sorry

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Then all you need to do is change the lambda expression to allow for the null value:

                    var ks = d.Max(v => v.Value == null ? 0.0 : v.Value\[0\]);
            

            The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            P 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Then all you need to do is change the lambda expression to allow for the null value:

                      var ks = d.Max(v => v.Value == null ? 0.0 : v.Value\[0\]);
              

              The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

              P Offline
              P Offline
              PozzaVecia
              wrote on last edited by
              #6

              thanks in this case I see you need a "hard coded" value 0.0. Is it possible to say "if null just skip it"?

              OriginalGriffO Richard DeemingR 2 Replies Last reply
              0
              • P PozzaVecia

                thanks in this case I see you need a "hard coded" value 0.0. Is it possible to say "if null just skip it"?

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                No, there is no way to "miss out" a value in a IEnumerable iteration. You could use double.MinValue instead:

                        var ks = d.Max(v => v.Value == null ? double.MinValue : v.Value\[0\]);
                

                or

                        var ks = d.Max(v => { if (v.Value == null) return null; return v.Value\[0\]; });
                

                But then you need to check for null in the ks variable as a list with all nulls with return a null instead of a double value.

                The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                Richard DeemingR 1 Reply Last reply
                0
                • P PozzaVecia

                  I need to instatiate a dictionary accepting null as value, but I have a syntax problem:

                  Dictionary d = new Dictionary()
                  d.Add("a", new double[2]{100,102});
                  d.Add("b", null);
                  d.Add("v", new double[2] { 99, 101 });;

                  Is it possible to do it?

                  J Offline
                  J Offline
                  Jay Nardev
                  wrote on last edited by
                  #8

                  use like this Dictionary[]> d = new Dictionary[]>(); d.Add("a", new Nullable[2] { 100, 102 }); d.Add("b", null); d.Add("v", new Nullable[2] { 99, 101 }); ; It would help you. :)

                  1 Reply Last reply
                  0
                  • P PozzaVecia

                    thanks in this case I see you need a "hard coded" value 0.0. Is it possible to say "if null just skip it"?

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

                    You can use the Where method[^] to skip the null items:

                    var ks = d.Where(v => v.Value != null).Max(v => v.Value[0]);


                    "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
                    • OriginalGriffO OriginalGriff

                      No, there is no way to "miss out" a value in a IEnumerable iteration. You could use double.MinValue instead:

                              var ks = d.Max(v => v.Value == null ? double.MinValue : v.Value\[0\]);
                      

                      or

                              var ks = d.Max(v => { if (v.Value == null) return null; return v.Value\[0\]; });
                      

                      But then you need to check for null in the ks variable as a list with all nulls with return a null instead of a double value.

                      The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

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

                      OriginalGriff wrote:

                      No, there is no way to "miss out" a value in a IEnumerable iteration.

                      Ahem![^] :rolleyes:


                      "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

                      OriginalGriffO 1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        OriginalGriff wrote:

                        No, there is no way to "miss out" a value in a IEnumerable iteration.

                        Ahem![^] :rolleyes:


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

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        Hadn't thought of Where - good point!

                        The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        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