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. Deep Thought OTD

Deep Thought OTD

Scheduled Pinned Locked Moved The Lounge
csharphelpquestion
35 Posts 12 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.
  • B Offline
    B Offline
    blackjack2150
    wrote on last edited by
    #1

    From StackOverflow comes this one: We noticed that lots of bug in our software developed in C# cause a NullReferenceException. Is there a reason why "null" has been included in the language? After all, if there were no "null", I would have no bug, right? In other words, what feature in the language couldn't work without null?

    R L D X A 7 Replies Last reply
    0
    • B blackjack2150

      From StackOverflow comes this one: We noticed that lots of bug in our software developed in C# cause a NullReferenceException. Is there a reason why "null" has been included in the language? After all, if there were no "null", I would have no bug, right? In other words, what feature in the language couldn't work without null?

      R Offline
      R Offline
      R Giskard Reventlov
      wrote on last edited by
      #2

      How do these people make a living???

      me, me, me

      R 1 Reply Last reply
      0
      • B blackjack2150

        From StackOverflow comes this one: We noticed that lots of bug in our software developed in C# cause a NullReferenceException. Is there a reason why "null" has been included in the language? After all, if there were no "null", I would have no bug, right? In other words, what feature in the language couldn't work without null?

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        What was even better is one of the answers, that is now deleted. It went something like: To make sure the instance has been correctly initialized. :omg: :wtf:

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)
        ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

        1 Reply Last reply
        0
        • B blackjack2150

          From StackOverflow comes this one: We noticed that lots of bug in our software developed in C# cause a NullReferenceException. Is there a reason why "null" has been included in the language? After all, if there were no "null", I would have no bug, right? In other words, what feature in the language couldn't work without null?

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #4

          It would have been possible to design C# without null. null is not necessary, it just got carried over from C++ and Java. But there are cases where null is useful - otherwise recursive data structures would be more difficult (how to signal the end of a linked list?); and often an additional "null" value makes sense (after all, that's why nullable value types were introduced). But I think that reference types shouldn't be nullable by default - make it explicit as with value types. Unfortunately, it's too late to change that.

          L L S 3 Replies Last reply
          0
          • B blackjack2150

            From StackOverflow comes this one: We noticed that lots of bug in our software developed in C# cause a NullReferenceException. Is there a reason why "null" has been included in the language? After all, if there were no "null", I would have no bug, right? In other words, what feature in the language couldn't work without null?

            X Offline
            X Offline
            Xiangyang Liu
            wrote on last edited by
            #5

            blackjack2150 wrote:

            In other words, what feature in the language couldn't work without null?

            None. Yes, null can be replaced with None, or better yet, Nothing (as in VB.NET). :-D

            My .NET Business Application Framework My Home Page My Younger Son & His "PET"

            L 1 Reply Last reply
            0
            • D Daniel Grunwald

              It would have been possible to design C# without null. null is not necessary, it just got carried over from C++ and Java. But there are cases where null is useful - otherwise recursive data structures would be more difficult (how to signal the end of a linked list?); and often an additional "null" value makes sense (after all, that's why nullable value types were introduced). But I think that reference types shouldn't be nullable by default - make it explicit as with value types. Unfortunately, it's too late to change that.

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Daniel Grunwald wrote:

              how to signal the end of a linked list?

              You do it like they taught you at school/university, with a Sentinal!

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)
              ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

              D B 2 Replies Last reply
              0
              • D Daniel Grunwald

                It would have been possible to design C# without null. null is not necessary, it just got carried over from C++ and Java. But there are cases where null is useful - otherwise recursive data structures would be more difficult (how to signal the end of a linked list?); and often an additional "null" value makes sense (after all, that's why nullable value types were introduced). But I think that reference types shouldn't be nullable by default - make it explicit as with value types. Unfortunately, it's too late to change that.

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

                ..and what would be the default value for, say; private object Test;

                D 1 Reply Last reply
                0
                • R R Giskard Reventlov

                  How do these people make a living???

                  me, me, me

                  R Offline
                  R Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  digital man wrote:

                  How do these people make a living???

                  By sweeping standing water off sidewalks. Programming is just a hobby for them.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  R 1 Reply Last reply
                  0
                  • L leppie

                    Daniel Grunwald wrote:

                    how to signal the end of a linked list?

                    You do it like they taught you at school/university, with a Sentinal!

                    xacc.ide - now with TabsToSpaces support
                    IronScheme - 1.0 alpha 4a out now (29 May 2008)
                    ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                    D Offline
                    D Offline
                    Daniel Grunwald
                    wrote on last edited by
                    #9

                    And what's the node after the sentinal? There are some possibilities here: 1. Use null. 2. Let the sentinal reference itself as "next" value. Great, you've just converted a NullReferenceException (which can be safely caught and handled) into a StackOverflowException! 3. Use inheritance so that only the non-sentinal nodes have a successor. This means all access to the "next" field must use casts. YUCK! However, this approach works great if the language supports discriminated unions[^].

                    L 1 Reply Last reply
                    0
                    • D Daniel Grunwald

                      And what's the node after the sentinal? There are some possibilities here: 1. Use null. 2. Let the sentinal reference itself as "next" value. Great, you've just converted a NullReferenceException (which can be safely caught and handled) into a StackOverflowException! 3. Use inheritance so that only the non-sentinal nodes have a successor. This means all access to the "next" field must use casts. YUCK! However, this approach works great if the language supports discriminated unions[^].

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #10

                      Daniel Grunwald wrote:

                      And what's the node after the sentinal?

                      Nothing, you simply use it as a special node, in fact all it's operations should throw exceptions.

                      xacc.ide - now with TabsToSpaces support
                      IronScheme - 1.0 alpha 4a out now (29 May 2008)
                      ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                      D 1 Reply Last reply
                      0
                      • L Lost User

                        ..and what would be the default value for, say; private object Test;

                        D Offline
                        D Offline
                        Daniel Grunwald
                        wrote on last edited by
                        #11

                        It wouldn't have a default value. You'd get a compiler error if you don't assign a value in the constructor. Just like the compiler error you get when you don't initialize all fields of a C# struct.

                        L 1 Reply Last reply
                        0
                        • X Xiangyang Liu

                          blackjack2150 wrote:

                          In other words, what feature in the language couldn't work without null?

                          None. Yes, null can be replaced with None, or better yet, Nothing (as in VB.NET). :-D

                          My .NET Business Application Framework My Home Page My Younger Son & His "PET"

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

                          And here I was thinking that the concept of NULL was mind-boggling only in SQL. :omg:

                          1 Reply Last reply
                          0
                          • L leppie

                            Daniel Grunwald wrote:

                            And what's the node after the sentinal?

                            Nothing, you simply use it as a special node, in fact all it's operations should throw exceptions.

                            xacc.ide - now with TabsToSpaces support
                            IronScheme - 1.0 alpha 4a out now (29 May 2008)
                            ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                            D Offline
                            D Offline
                            Daniel Grunwald
                            wrote on last edited by
                            #13

                            So basically:

                            interface INode {
                            INode Next { get; }
                            object Value { get; }
                            }

                            class Node : INode {
                            public INode Next { get; set; }
                            public object Value { get; set; }
                            }
                            class SentinelNode : INode {
                            public INode Next { get { throw new NotSupportedException(); } }
                            public object Value { get { throw new NotSupportedException(); } }
                            }

                            How's that any better than the language-provided "sentinel" null that throws a NullReferenceException on property access? You still have the same problem: The interface looks like you could get the next node; but in fact, you cannot. The only way I can think of to solve this in a type-safe manner without exceptions is with discriminated unions.

                            L 1 Reply Last reply
                            0
                            • D Daniel Grunwald

                              It wouldn't have a default value. You'd get a compiler error if you don't assign a value in the constructor. Just like the compiler error you get when you don't initialize all fields of a C# struct.

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

                              :mad: ..declare a generic list of forms; x = new List

                              (100); Now, do I really want 100 instantiated Forms? Null means "not there", which isn't the same as an 'empty' or default object. There is an easier way to prevent bugs from null-references: don't program :)

                              D 1 Reply Last reply
                              0
                              • L Lost User

                                :mad: ..declare a generic list of forms; x = new List

                                (100); Now, do I really want 100 instantiated Forms? Null means "not there", which isn't the same as an 'empty' or default object. There is an easier way to prevent bugs from null-references: don't program :)

                                D Offline
                                D Offline
                                Daniel Grunwald
                                wrote on last edited by
                                #15

                                eddyvluggen wrote:

                                x = new List

                                (100);

                                That creates an empty List<form> with a capacity of 100. No nulls involved. OK, there may be nulls in the internal array representation, but the public interface of List<T> doesn't require nulls in any way. Otherwise you couldn't use List<T> with value types...

                                L 1 Reply Last reply
                                0
                                • L leppie

                                  Daniel Grunwald wrote:

                                  how to signal the end of a linked list?

                                  You do it like they taught you at school/university, with a Sentinal!

                                  xacc.ide - now with TabsToSpaces support
                                  IronScheme - 1.0 alpha 4a out now (29 May 2008)
                                  ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                                  B Offline
                                  B Offline
                                  Brady Kelly
                                  wrote on last edited by
                                  #16

                                  I was taught at university that a null signalled the end.

                                  I've Found My Mojo

                                  L 2 Replies Last reply
                                  0
                                  • D Daniel Grunwald

                                    eddyvluggen wrote:

                                    x = new List

                                    (100);

                                    That creates an empty List<form> with a capacity of 100. No nulls involved. OK, there may be nulls in the internal array representation, but the public interface of List<T> doesn't require nulls in any way. Otherwise you couldn't use List<T> with value types...

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

                                    I agree, you can't avoid using null, unless you cripple the language. You don't want to instantiate a class and hogg memory when a reference (say, a pointer) to 'null' would suffice. Another argument comes from the MS help on 'null'; // Set mc to null again. The object it referenced // is no longer accsessible and can now be garbage-collected. mc = null; How would this work with a 'default' object? Removing the null-keyword from the language doesn't reduce errors. It would be as ridiculous as removing all keys from a database, in order to prevent key-errors :sigh:

                                    D L 2 Replies Last reply
                                    0
                                    • D Daniel Grunwald

                                      So basically:

                                      interface INode {
                                      INode Next { get; }
                                      object Value { get; }
                                      }

                                      class Node : INode {
                                      public INode Next { get; set; }
                                      public object Value { get; set; }
                                      }
                                      class SentinelNode : INode {
                                      public INode Next { get { throw new NotSupportedException(); } }
                                      public object Value { get { throw new NotSupportedException(); } }
                                      }

                                      How's that any better than the language-provided "sentinel" null that throws a NullReferenceException on property access? You still have the same problem: The interface looks like you could get the next node; but in fact, you cannot. The only way I can think of to solve this in a type-safe manner without exceptions is with discriminated unions.

                                      L Offline
                                      L Offline
                                      leppie
                                      wrote on last edited by
                                      #18

                                      The Sentinel node is never exposed, and IIRC neither is a Node, they are internal to the implementation and the user should not have to worry about it. You simply use the LinkedList interface (yeah very Java'ish). Anyways, this was an example of what I got thought (personally I would just go for a null).

                                      xacc.ide - now with TabsToSpaces support
                                      IronScheme - 1.0 beta 1 - coming soon
                                      ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                                      C 1 Reply Last reply
                                      0
                                      • L Lost User

                                        I agree, you can't avoid using null, unless you cripple the language. You don't want to instantiate a class and hogg memory when a reference (say, a pointer) to 'null' would suffice. Another argument comes from the MS help on 'null'; // Set mc to null again. The object it referenced // is no longer accsessible and can now be garbage-collected. mc = null; How would this work with a 'default' object? Removing the null-keyword from the language doesn't reduce errors. It would be as ridiculous as removing all keys from a database, in order to prevent key-errors :sigh:

                                        D Offline
                                        D Offline
                                        Daniel Grunwald
                                        wrote on last edited by
                                        #19

                                        But while null is useful for some data structures (internal implementation of List<T>), there's no reason why all references are nullable. The languages forces us to think "can 'a' be null" whenever we write "a.b" (and a is a reference type). That's a big design mistake in the C# language.

                                        Anders Hejlsberg wrote:

                                        Would you do anything differently in developing C# if you had the chance? ...(snip)... With language design or with platform design 1.0 is always a unique opportunity to put down your core values, your core designs, and then with every version thereafter it’s much harder to fundamentally change the nature of the beast. And so, the things that you typically end up regretting later are the fundamentals that you didn’t quite get right. Because those you can’t change - you can always ship new libraries etc, but you can’t change the fundamental gestalt of the platform. For example, in the type system we do not have separation between value and reference types and nullability of types. This may sound a little wonky or a little technical, but in C# reference types can be null, such as strings, but value types cannot be null. It sure would be nice to have had non-nullable reference types, so you could declare that ‘this string can never be null, and I want you compiler to check that I can never hit a null pointer here’. 50% of the bugs that people run into today, coding with C# in our platform, and the same is true of Java for that matter, are probably null reference exceptions. If we had had a stronger type system that would allow you to say that ‘this parameter may never be null, and you compiler please check that at every call, by doing static analysis of the code’. Then we could have stamped out classes of bugs.

                                        L 1 Reply Last reply
                                        0
                                        • L Lost User

                                          I agree, you can't avoid using null, unless you cripple the language. You don't want to instantiate a class and hogg memory when a reference (say, a pointer) to 'null' would suffice. Another argument comes from the MS help on 'null'; // Set mc to null again. The object it referenced // is no longer accsessible and can now be garbage-collected. mc = null; How would this work with a 'default' object? Removing the null-keyword from the language doesn't reduce errors. It would be as ridiculous as removing all keys from a database, in order to prevent key-errors :sigh:

                                          L Offline
                                          L Offline
                                          leppie
                                          wrote on last edited by
                                          #20

                                          eddyvluggen wrote:

                                          It would be as ridiculous as removing all keys from a database, in order to prevent key-errors

                                          What?!#!? You mean DBA's dont do that already??? ;P

                                          xacc.ide - now with TabsToSpaces support
                                          IronScheme - 1.0 beta 1 - coming soon
                                          ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                                          C 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