Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# vs VB.NET - Handling of null Structures

Tags:

I ran across this and was wondering if someone could explain why this works in VB.NET when I would expect it should fail, just like it does in C#

//The C# Version  struct Person {     public string name; } ... Person someone = null; //Nope! Can't do that!! Person? someoneElse = null; //No problem, just like expected 

But then in VB.NET...

Structure Person     Public name As String End Structure ... Dim someone As Person = Nothing 'Wha? this is okay? 

Is Nothing not the same as null (Nothing != null - LOL?), or is this just different ways of handling the same situation between the two languages?

Why or what is handled differently between the two that makes this okay in one, but not the other?

[Update]

Given some of the comments, I messed with this a bit more... it seems as if you actually have to use Nullable if you want to allow something to be null in VB.NET... so for example...

'This is false - It is still a person' Dim someone As Person = Nothing Dim isSomeoneNull As Boolean = someone.Equals(Nothing) 'false'  'This is true - the result is actually nullable now' Dim someoneElse As Nullable(Of Person) = Nothing Dim isSomeoneElseNull As Boolean = someoneElse.Equals(Nothing) 'true' 

Too weird...

like image 474
hugoware Avatar asked Nov 19 '08 21:11

hugoware


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

If I remember correctly, 'Nothing' in VB means "the default value". For a value type, that's the default value, for a reference type, that would be null. Thus, assigning nothing to a struct, is no problem at all.

like image 65
BFree Avatar answered Sep 22 '22 04:09

BFree