Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

C# uses references instead of pointers. Are C# references the same as C++ references?

C# pointers references uses
0
Posted

C# uses references instead of pointers. Are C# references the same as C++ references?

0

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. If you try to use a null reference, a NullReferenceException is thrown. For example, look at the following method: void displayStringLength( string s ) { Console.WriteLine( “String is length {0}”, s.Length ); } The problem with this method is that it will throw a NullReferenceException if called like this: string s = null; displayStringLength( s ); 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: void displayStringLength( string s ) { if( s == null ) Console.WriteLine( “String is null” ); else Console.WriteLine( “String is length {0}”, s.Length ); } 3.

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123