InterviewSolution
| 1. |
Are C# References The Same As C++ References? |
|
Answer» Not quite. The basic idea is the same, but one significant DIFFERENCE is that C# references can be null . So you cannot rely on a C# reference pointing to a valid object. In that respect a C# reference is more LIKE a C++ pointer than a C++ reference. If you try to use a null reference, a NullReferenceException is thrown. For example, look at the following method: Of course for some SITUATIONS you may deem a NullReferenceException to be a perfectly acceptable outcome, but in this case it might be better to re-write the method like this: Not quite. The basic idea is the same, but one significant difference is that C# references can be null . So you cannot rely on a C# reference pointing to a valid object. In that respect a C# reference is more like a C++ pointer than a C++ reference. If you try to use a null reference, a NullReferenceException is thrown. For example, look at the following method: Of course for some situations you may deem a NullReferenceException to be a perfectly acceptable outcome, but in this case it might be better to re-write the method like this: |
|