code_junkie

C# vs VB.NET - Handling of null Structures

Nov 12th, 2011
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. //The C# Version
  2.  
  3. struct Person {
  4. public string name;
  5. }
  6. ...
  7. Person someone = null; //Nope! Can't do that!!
  8. Person? someoneElse = null; //No problem, just like expected
  9.  
  10. Structure Person
  11. Public name As String
  12. End Structure
  13. ...
  14. Dim someone As Person = Nothing 'Wha? this is okay?
  15.  
  16. 'This is false - It is still a person'
  17. Dim someone As Person = Nothing
  18. Dim isSomeoneNull As Boolean = someone.Equals(Nothing) 'false'
  19.  
  20. 'This is true - the result is actually nullable now'
  21. Dim someoneElse As Nullable(Of Person) = Nothing
  22. Dim isSomeoneElseNull As Boolean = someoneElse.Equals(Nothing) 'true'
Add Comment
Please, Sign In to add comment