Click here to Skip to main content
15,890,282 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# versus C++, null references and protecting against them Pin
bob1697214-Jan-11 10:23
bob1697214-Jan-11 10:23 
GeneralRe: C# versus C++, null references and protecting against them Pin
jschell15-Jan-11 9:44
jschell15-Jan-11 9:44 
AnswerRe: C# versus C++, null references and protecting against them Pin
Paul Michalik15-Jan-11 2:23
Paul Michalik15-Jan-11 2:23 
AnswerRe: C# versus C++, null references and protecting against them Pin
PIEBALDconsult15-Jan-11 4:12
mvePIEBALDconsult15-Jan-11 4:12 
GeneralRe: C# versus C++, null references and protecting against them Pin
bob1697215-Jan-11 5:00
bob1697215-Jan-11 5:00 
AnswerRe: C# versus C++, null references and protecting against them Pin
Chuck O'Toole15-Jan-11 17:14
Chuck O'Toole15-Jan-11 17:14 
GeneralRe: C# versus C++, null references and protecting against them Pin
bob1697215-Jan-11 19:23
bob1697215-Jan-11 19:23 
GeneralRe: C# versus C++, null references and protecting against them Pin
Chuck O'Toole15-Jan-11 20:56
Chuck O'Toole15-Jan-11 20:56 
Well, I was simply reacting to your statement in the original post,
/*
No need to check the reference before using it
since references cannot be null.
*/
In my experience, when some code assumes things cannot be null, that's exactly where to start looking for bugs. And if you're assuming that a "by reference" specification removes some level of bug enough that you can remove the ASSERT / checks, then I wanted to show an example (real, not concocted) that shows the assumption to be false.

And then you asked for a way to eliminate such checks from C# code like you could from C++ code, which I infer to mean that you have such checks, find them somewhat inefficient and/or redundant in well behaved programs, and would like to remove them using some help from the language. Again, I wanted to point out that the C++ language does not give you any help either and that your trust in it was misplaced.

Now your reply says
bob16972 wrote:
the violation occurs by the caller and before the prolog of the method completes. If the caller violates the standard definition of a reference, there is nothing I can do about it. I'm not even sure there is a way to check the reference for validity since the result of the action you described would be undefined (or at least, so it seems).

which seems like an reasonable argument to remove all checks anyway since there's nothing you can do when the caller violates some rules, including the assumed rule that they'll pass something reasonable.

Look, all I'm saying is that if you are writing public API entries, for other programmers to call, you're better off including the checks all the time. Yes, "null pointers" are just one element in the set of all possible bad arguments and checking for them will never catch the case of the pointer being 0x00000001 or 0xcdcdcdcd (assuming 32-bit pointers) but ASSERTs may catch enough during debugging that may make debugging easier.
GeneralRe: C# versus C++, null references and protecting against them Pin
bob1697216-Jan-11 5:24
bob1697216-Jan-11 5:24 
GeneralRe: C# versus C++, null references and protecting against them Pin
Chuck O'Toole16-Jan-11 6:00
Chuck O'Toole16-Jan-11 6:00 
GeneralRe: C# versus C++, null references and protecting against them Pin
Paul Michalik16-Jan-11 7:39
Paul Michalik16-Jan-11 7:39 
GeneralRe: C# versus C++, null references and protecting against them Pin
Chuck O'Toole16-Jan-11 7:57
Chuck O'Toole16-Jan-11 7:57 
GeneralRe: C# versus C++, null references and protecting against them Pin
Chuck O'Toole16-Jan-11 8:30
Chuck O'Toole16-Jan-11 8:30 
GeneralRe: C# versus C++, null references and protecting against them Pin
bob1697216-Jan-11 12:29
bob1697216-Jan-11 12:29 
GeneralRe: C# versus C++, null references and protecting against them Pin
Paul Michalik16-Jan-11 20:35
Paul Michalik16-Jan-11 20:35 
GeneralRe: C# versus C++, null references and protecting against them Pin
Paul Michalik15-Jan-11 23:04
Paul Michalik15-Jan-11 23:04 
GeneralRe: C# versus C++, null references and protecting against them Pin
bob1697216-Jan-11 4:57
bob1697216-Jan-11 4:57 
QuestionCOM error [modified] Pin
polycom12314-Jan-11 8:05
polycom12314-Jan-11 8:05 
QuestionDynamic assembly information Pin
Helfdane14-Jan-11 2:32
Helfdane14-Jan-11 2:32 
AnswerRe: Dynamic assembly information Pin
Ravi Sant14-Jan-11 3:49
Ravi Sant14-Jan-11 3:49 
GeneralRe: Dynamic assembly information Pin
Helfdane16-Jan-11 21:05
Helfdane16-Jan-11 21:05 
AnswerRe: Dynamic assembly information Pin
PIEBALDconsult15-Jan-11 9:08
mvePIEBALDconsult15-Jan-11 9:08 
GeneralRe: Dynamic assembly information Pin
Tony Richards16-Jan-11 8:54
Tony Richards16-Jan-11 8:54 
GeneralRe: Dynamic assembly information Pin
PIEBALDconsult16-Jan-11 14:49
mvePIEBALDconsult16-Jan-11 14:49 
QuestionRunning an exe file ( I mean a program) from inside my own program Pin
Fred 3414-Jan-11 1:41
Fred 3414-Jan-11 1:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.