Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interview Question in C#

A Technical Lead asked me the following:
He created a class, declared an object and initialized it. But in some circumstance we may get "null reference" exception.
He commented that there are 1000 possible reasons for such exception and asked me to guess a single reason.
I am unable to figure it out. What is (are) the reason(s) ,we may get such an exception?

like image 428
amutha Avatar asked Apr 01 '10 13:04

amutha


People also ask

What are the important topics in C language for interview?

The most commonly used built-in functions in C are scanf(), printf(), strcpy, strlwr, strcmp, strlen, strcat, and many more. Built-function is also known as library functions that are provided by the system to make the life of a developer easy by assisting them to do certain commonly used predefined tasks.

What is basic C concept?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

  1. You have used an object reference you have explicitly set to null, or
  2. You have used an object reference you have implicitly set to null, or
  3. Somewhere in your code, or in code called by you, there is the statement throw new NullReferenceException() (which you shouldn't do, by the way). I don't know if this counts, since it's not a real null reference.

I can't think of any of the other 997 reasons.

Edit: Thanks, Mark Byers, for point 3.

like image 149
erikkallen Avatar answered Oct 25 '22 15:10

erikkallen


If it's a multi-threaded app, then some other thread could come along and set the object to a null reference.

like image 38
Matthew Groves Avatar answered Oct 25 '22 14:10

Matthew Groves