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. The Lounge
  3. Another reason I don't like LINQ

Another reason I don't like LINQ

Scheduled Pinned Locked Moved The Lounge
csharplinqdebugginghelpquestion
41 Posts 19 Posters 3 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.
  • H Offline
    H Offline
    honey the codewitch
    wrote on last edited by
    #1

    Here's my error

    System.ArgumentNullException: 'Value cannot be null.
    Parameter name: values'

    ... for this mess:

    Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
    {
    return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
    }).Select(p =>
    {
    return new ColumnHeader()
    {
    Name = p.Name,
    Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
    };
    }).ToArray());

    The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

    Real programmers use butterflies

    S Greg UtasG L Richard DeemingR M 15 Replies Last reply
    0
    • H honey the codewitch

      Here's my error

      System.ArgumentNullException: 'Value cannot be null.
      Parameter name: values'

      ... for this mess:

      Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
      {
      return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
      }).Select(p =>
      {
      return new ColumnHeader()
      {
      Name = p.Name,
      Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
      };
      }).ToArray());

      The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

      Real programmers use butterflies

      S Offline
      S Offline
      Slacker007
      wrote on last edited by
      #2

      I love LINQ. Developers have been using it for years now, with great success. Sorry you have found another technology that you hate.

      H 1 Reply Last reply
      0
      • H honey the codewitch

        Here's my error

        System.ArgumentNullException: 'Value cannot be null.
        Parameter name: values'

        ... for this mess:

        Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
        {
        return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
        }).Select(p =>
        {
        return new ColumnHeader()
        {
        Name = p.Name,
        Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
        };
        }).ToArray());

        The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

        Real programmers use butterflies

        Greg UtasG Offline
        Greg UtasG Offline
        Greg Utas
        wrote on last edited by
        #3

        Looks like it's trying to compete with compiler error messages involving C++ templates. :-D

        Robust Services Core | Software Techniques for Lemmings | Articles
        The fox knows many things, but the hedgehog knows one big thing.

        <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
        <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

        H 1 Reply Last reply
        0
        • H honey the codewitch

          Here's my error

          System.ArgumentNullException: 'Value cannot be null.
          Parameter name: values'

          ... for this mess:

          Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
          {
          return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
          }).Select(p =>
          {
          return new ColumnHeader()
          {
          Name = p.Name,
          Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
          };
          }).ToArray());

          The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

          Real programmers use butterflies

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

          Reminds me of people who don't use paragraphs.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          • H honey the codewitch

            Here's my error

            System.ArgumentNullException: 'Value cannot be null.
            Parameter name: values'

            ... for this mess:

            Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
            {
            return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
            }).Select(p =>
            {
            return new ColumnHeader()
            {
            Name = p.Name,
            Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
            };
            }).ToArray());

            The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

            Real programmers use butterflies

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

            My crystal ball says your object's type doesn't contain any generic arguments, so you're passing null to AddRange.

            var genericArguments = obj.GetType().GetGenericArguments();
            if (genericArguments.Length != 0)
            {
            Columns.AddRange(genericArguments[0].GetProperties()
            .Where(p => p.GetCustomAttributes(true).OfType<BrowsableAttribute>().FirstOrDefault()?.Browsable ?? DefaultBrowsableState)
            .Select(p => new ColumnHeader
            {
            Name = p.Name,
            Text = p.GetCustomAttributes(true).OfType<DisplayNameAttribute>().FirstOrDefault()?.DisplayName ?? p.Name,
            });
            }

            Throw in a custom extension method to simplify it slightly:

            public static class AttributeExtensions
            {
            public static TAttribute GetCustomAttribute<TAttribute>(this ICustomAttributeProvider value, bool inherit = true)
            where TAttribute : Attribute
            {
            if (value is null) throw new ArgumentNullException(nameof(value));

                object\[\] attributes = value.GetCustomAttributes(typeof(TAttribute), inherit);
                return attributes.Length == 0 ? null : (TAttribute)attributes\[0\];
            }
            

            }

            var genericArguments = obj.GetType().GetGenericArguments();
            if (genericArguments.Length != 0)
            {
            Columns.AddRange(genericArguments[0].GetProperties()
            .Where(p => p.GetCustomAttribute<BrowsableAttribute>()?.Browsable ?? DefaultBrowsableState)
            .Select(p => new ColumnHeader
            {
            Name = p.Name,
            Text = p.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName ?? p.Name,
            });
            }


            "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

            H 1 Reply Last reply
            0
            • H honey the codewitch

              Here's my error

              System.ArgumentNullException: 'Value cannot be null.
              Parameter name: values'

              ... for this mess:

              Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
              {
              return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
              }).Select(p =>
              {
              return new ColumnHeader()
              {
              Name = p.Name,
              Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
              };
              }).ToArray());

              The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

              Real programmers use butterflies

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              Which functions takes a "values" parameter? I would guess it's `Columns.AddRange()`? If so, then my first guess would be that the problem is with `obj.GetType().GetGenericArguments()` not having any items (length == 0). I guess this is one of the reasons we try to write readable code... so we also get more specific error messages. This one-liner nonsense is bad for everyone.

              1 Reply Last reply
              0
              • H honey the codewitch

                Here's my error

                System.ArgumentNullException: 'Value cannot be null.
                Parameter name: values'

                ... for this mess:

                Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
                {
                return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
                }).Select(p =>
                {
                return new ColumnHeader()
                {
                Name = p.Name,
                Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
                };
                }).ToArray());

                The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

                Real programmers use butterflies

                O Offline
                O Offline
                obermd
                wrote on last edited by
                #7

                The underlying issue is that this garbage is a "one-liner". One-liners are darn near impossible to debug. LinQ invites, and even encourages this crap into the code.

                L M 2 Replies Last reply
                0
                • O obermd

                  The underlying issue is that this garbage is a "one-liner". One-liners are darn near impossible to debug. LinQ invites, and even encourages this crap into the code.

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

                  As does SQL. If a given statement works, just keep piling on until it doesn't, then post it in Q&A.

                  It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    My crystal ball says your object's type doesn't contain any generic arguments, so you're passing null to AddRange.

                    var genericArguments = obj.GetType().GetGenericArguments();
                    if (genericArguments.Length != 0)
                    {
                    Columns.AddRange(genericArguments[0].GetProperties()
                    .Where(p => p.GetCustomAttributes(true).OfType<BrowsableAttribute>().FirstOrDefault()?.Browsable ?? DefaultBrowsableState)
                    .Select(p => new ColumnHeader
                    {
                    Name = p.Name,
                    Text = p.GetCustomAttributes(true).OfType<DisplayNameAttribute>().FirstOrDefault()?.DisplayName ?? p.Name,
                    });
                    }

                    Throw in a custom extension method to simplify it slightly:

                    public static class AttributeExtensions
                    {
                    public static TAttribute GetCustomAttribute<TAttribute>(this ICustomAttributeProvider value, bool inherit = true)
                    where TAttribute : Attribute
                    {
                    if (value is null) throw new ArgumentNullException(nameof(value));

                        object\[\] attributes = value.GetCustomAttributes(typeof(TAttribute), inherit);
                        return attributes.Length == 0 ? null : (TAttribute)attributes\[0\];
                    }
                    

                    }

                    var genericArguments = obj.GetType().GetGenericArguments();
                    if (genericArguments.Length != 0)
                    {
                    Columns.AddRange(genericArguments[0].GetProperties()
                    .Where(p => p.GetCustomAttribute<BrowsableAttribute>()?.Browsable ?? DefaultBrowsableState)
                    .Select(p => new ColumnHeader
                    {
                    Name = p.Name,
                    Text = p.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName ?? p.Name,
                    });
                    }


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

                    H Offline
                    H Offline
                    honey the codewitch
                    wrote on last edited by
                    #9

                    Yeah I sorted it out. It was being applied to an instance of the wrong class. The code that's using it is ridiculously complicated, and something small was out of place. This error was the end result. I still think it's suitable for the purposes of this rant. :) Such is life sometimes. I'm working with lots of Other People's Code(TM) at the moment. It's not so much that any one of them is particularly bad, so much as gluing together so many different paradigms is well.. as you can expect. But the main complication of it all is making it designable so my client can open it up in visual studio and tweak it, because he likes to be able to. He can code some, but I'd prefer he keep his mitts off what i write. I can deal with him using the designer. It works for both of us because he's afraid of my code anyway, and that way he doesn't have to bug me for little changes, but sometimes the code to make it all go properly is nasty.

                    Real programmers use butterflies

                    Sander RosselS 1 Reply Last reply
                    0
                    • S Slacker007

                      I love LINQ. Developers have been using it for years now, with great success. Sorry you have found another technology that you hate.

                      H Offline
                      H Offline
                      honey the codewitch
                      wrote on last edited by
                      #10

                      There's a pretty large gulf between not liking something and hating it. If I don't like it, it just means I'll avoid using it, and sometimes complain about it. If I hated it I'd probably actively seek to destroy it somehow, and that would be a fight with Microsoft I'd rather not invest in, especially since it's one I couldn't win.

                      Real programmers use butterflies

                      J 1 Reply Last reply
                      0
                      • Greg UtasG Greg Utas

                        Looks like it's trying to compete with compiler error messages involving C++ templates. :-D

                        Robust Services Core | Software Techniques for Lemmings | Articles
                        The fox knows many things, but the hedgehog knows one big thing.

                        H Offline
                        H Offline
                        honey the codewitch
                        wrote on last edited by
                        #11

                        ;P There are so many things not to like about C++ templates but I love them anyway. It's shameful, my inconsistencies in this regard. :) I suppose it's because LINQ wasn't part of C# since the beginning, I didn't consider C# and LINQ to be irrevocably linked (pardon the pun) the way I do with C++ and templates. It's part of the language, not a "bag on the side". That probably contributes to the difference in attitude I have toward each. But also a little inconsistency never really bothered me that much.

                        Real programmers use butterflies

                        D 1 Reply Last reply
                        0
                        • H honey the codewitch

                          ;P There are so many things not to like about C++ templates but I love them anyway. It's shameful, my inconsistencies in this regard. :) I suppose it's because LINQ wasn't part of C# since the beginning, I didn't consider C# and LINQ to be irrevocably linked (pardon the pun) the way I do with C++ and templates. It's part of the language, not a "bag on the side". That probably contributes to the difference in attitude I have toward each. But also a little inconsistency never really bothered me that much.

                          Real programmers use butterflies

                          D Offline
                          D Offline
                          Daniel Pfeffer
                          wrote on last edited by
                          #12

                          honey the codewitch wrote:

                          But also a little inconsistency never really bothered me that much.

                          A foolish consistency is the hobgoblin of small minds. -- Emerson :)

                          Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                          1 Reply Last reply
                          0
                          • H honey the codewitch

                            Here's my error

                            System.ArgumentNullException: 'Value cannot be null.
                            Parameter name: values'

                            ... for this mess:

                            Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
                            {
                            return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
                            }).Select(p =>
                            {
                            return new ColumnHeader()
                            {
                            Name = p.Name,
                            Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
                            };
                            }).ToArray());

                            The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

                            Real programmers use butterflies

                            M Offline
                            M Offline
                            Matthew Dennis
                            wrote on last edited by
                            #13

                            I find that adding line breaks makes it a lot easier to read. Remember, you are writing for the next person to touch the code, not the computer. It is missing some Elvis operators, before the Where, the Select, and the ToArray, along with providing a value if the null propagates to the end. Also, you don't need the ToArray() as AddRange takes an IEnumerable<T>.

                            Columns
                            .AddRange(
                            obj.GetType()
                            .GetGenericArguments()
                            .FirstOrDefault()?.GetProperties()

                            // need the Elvis operator 
                            ?.Where(p =>
                            {
                                return p.GetCustomAttributes(true)
                                        .OfType()
                                        .FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
                            })
                            
                            // need the Elvis operator 
                            ?.Select(p =>
                            {
                                return new ColumnHeader()
                                {
                                    Name = p.Name,
                                    Text = p.GetCustomAttributes(true)
                                            .OfType().FirstOrDefault()?.DisplayName ?? p.Name
                                };
                            })
                            
                            // don't need ToArray(), but need to provide a non-null value for AddRange()
                            ?? Array.Empty()
                            

                            );

                            "Time flies like an arrow. Fruit flies like a banana."

                            H S Richard DeemingR 3 Replies Last reply
                            0
                            • H honey the codewitch

                              Here's my error

                              System.ArgumentNullException: 'Value cannot be null.
                              Parameter name: values'

                              ... for this mess:

                              Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
                              {
                              return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
                              }).Select(p =>
                              {
                              return new ColumnHeader()
                              {
                              Name = p.Name,
                              Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
                              };
                              }).ToArray());

                              The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

                              Real programmers use butterflies

                              M Offline
                              M Offline
                              Maximilien
                              wrote on last edited by
                              #14

                              I'm not an expert or even a novice ... (serious) Can't you just "unLINQ" this and find and fix the error and LINQ it up again ?

                              I'd rather be phishing!

                              H 1 Reply Last reply
                              0
                              • M Maximilien

                                I'm not an expert or even a novice ... (serious) Can't you just "unLINQ" this and find and fix the error and LINQ it up again ?

                                I'd rather be phishing!

                                H Offline
                                H Offline
                                honey the codewitch
                                wrote on last edited by
                                #15

                                Yes. But that's my point. To get a reasonable error message out of LINQ the solution is do it without LINQ. meh.

                                Real programmers use butterflies

                                1 Reply Last reply
                                0
                                • M Matthew Dennis

                                  I find that adding line breaks makes it a lot easier to read. Remember, you are writing for the next person to touch the code, not the computer. It is missing some Elvis operators, before the Where, the Select, and the ToArray, along with providing a value if the null propagates to the end. Also, you don't need the ToArray() as AddRange takes an IEnumerable<T>.

                                  Columns
                                  .AddRange(
                                  obj.GetType()
                                  .GetGenericArguments()
                                  .FirstOrDefault()?.GetProperties()

                                  // need the Elvis operator 
                                  ?.Where(p =>
                                  {
                                      return p.GetCustomAttributes(true)
                                              .OfType()
                                              .FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
                                  })
                                  
                                  // need the Elvis operator 
                                  ?.Select(p =>
                                  {
                                      return new ColumnHeader()
                                      {
                                          Name = p.Name,
                                          Text = p.GetCustomAttributes(true)
                                                  .OfType().FirstOrDefault()?.DisplayName ?? p.Name
                                      };
                                  })
                                  
                                  // don't need ToArray(), but need to provide a non-null value for AddRange()
                                  ?? Array.Empty()
                                  

                                  );

                                  "Time flies like an arrow. Fruit flies like a banana."

                                  H Offline
                                  H Offline
                                  honey the codewitch
                                  wrote on last edited by
                                  #16

                                  In my defense I didn't write that, nor run it through autoformat yet.

                                  Real programmers use butterflies

                                  1 Reply Last reply
                                  0
                                  • H honey the codewitch

                                    Here's my error

                                    System.ArgumentNullException: 'Value cannot be null.
                                    Parameter name: values'

                                    ... for this mess:

                                    Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
                                    {
                                    return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
                                    }).Select(p =>
                                    {
                                    return new ColumnHeader()
                                    {
                                    Name = p.Name,
                                    Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
                                    };
                                    }).ToArray());

                                    The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

                                    Real programmers use butterflies

                                    F Offline
                                    F Offline
                                    F ES Sitecore
                                    wrote on last edited by
                                    #17

                                    It's not really LINQ's fault, it's the chaining of multiple commands into a single statement and also a little of your own inexperience; given that error message many would know exactly where to look.

                                    1 Reply Last reply
                                    0
                                    • H honey the codewitch

                                      Here's my error

                                      System.ArgumentNullException: 'Value cannot be null.
                                      Parameter name: values'

                                      ... for this mess:

                                      Columns.AddRange(obj.GetType().GetGenericArguments().FirstOrDefault()?.GetProperties().Where(p =>
                                      {
                                      return p.GetCustomAttributes(true).OfType().FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
                                      }).Select(p =>
                                      {
                                      return new ColumnHeader()
                                      {
                                      Name = p.Name,
                                      Text = p.GetCustomAttributes(true).OfType().FirstOrDefault()?.DisplayName ?? p.Name
                                      };
                                      }).ToArray());

                                      The thing is, I know what it's trying to do, and the code makes sense to me even though I didn't write it. The LINQ isn't really that bad here. But the error message is just awful. I don't even know where to begin. Time to hand roll the same statement LINQless so I can debug it. So consider this my part 2 in why LINQ is for the birds.

                                      Real programmers use butterflies

                                      R Offline
                                      R Offline
                                      Ryan Peden
                                      wrote on last edited by
                                      #18

                                      A couple of possibly interesting bits of feedback, assuming that code comes from here: - Adding it to a WinForms app created with .NET Core 3.1 or .NET 5 and turning on nullable reference types finds 17 potential accidental nulls in the code from that SO post. But the Columns.AddRange call itself isn't one of them because WinForms wasn't built with NRT enabled. So the compiler decides it can't say one way or another if passing a null values argument to AddRange is okay. - Resharper catches the potential error whether you're using .NET Core/.NET 5 or .NET Framework. It even suggests a fix. The static analysis it's doing must look at AddRange and notice that the first thing that method does is throw an exception if values is null.

                                      S J Richard DeemingR H 4 Replies Last reply
                                      0
                                      • M Matthew Dennis

                                        I find that adding line breaks makes it a lot easier to read. Remember, you are writing for the next person to touch the code, not the computer. It is missing some Elvis operators, before the Where, the Select, and the ToArray, along with providing a value if the null propagates to the end. Also, you don't need the ToArray() as AddRange takes an IEnumerable<T>.

                                        Columns
                                        .AddRange(
                                        obj.GetType()
                                        .GetGenericArguments()
                                        .FirstOrDefault()?.GetProperties()

                                        // need the Elvis operator 
                                        ?.Where(p =>
                                        {
                                            return p.GetCustomAttributes(true)
                                                    .OfType()
                                                    .FirstOrDefault()?.Browsable ?? DefaultBrowsableState;
                                        })
                                        
                                        // need the Elvis operator 
                                        ?.Select(p =>
                                        {
                                            return new ColumnHeader()
                                            {
                                                Name = p.Name,
                                                Text = p.GetCustomAttributes(true)
                                                        .OfType().FirstOrDefault()?.DisplayName ?? p.Name
                                            };
                                        })
                                        
                                        // don't need ToArray(), but need to provide a non-null value for AddRange()
                                        ?? Array.Empty()
                                        

                                        );

                                        "Time flies like an arrow. Fruit flies like a banana."

                                        S Offline
                                        S Offline
                                        Slacker007
                                        wrote on last edited by
                                        #19

                                        Matthew Dennis wrote:

                                        I find that adding line breaks makes it a lot easier to read

                                        :thumbsup:

                                        1 Reply Last reply
                                        0
                                        • R Ryan Peden

                                          A couple of possibly interesting bits of feedback, assuming that code comes from here: - Adding it to a WinForms app created with .NET Core 3.1 or .NET 5 and turning on nullable reference types finds 17 potential accidental nulls in the code from that SO post. But the Columns.AddRange call itself isn't one of them because WinForms wasn't built with NRT enabled. So the compiler decides it can't say one way or another if passing a null values argument to AddRange is okay. - Resharper catches the potential error whether you're using .NET Core/.NET 5 or .NET Framework. It even suggests a fix. The static analysis it's doing must look at AddRange and notice that the first thing that method does is throw an exception if values is null.

                                          S Offline
                                          S Offline
                                          Slacker007
                                          wrote on last edited by
                                          #20

                                          Ryan Peden wrote:

                                          Resharper catches the potential error

                                          Ryan Peden wrote:

                                          It even suggests a fix.

                                          :thumbsup:

                                          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