17

Possible Duplicate:
Default value for generics

OK, so while translating some code from C# to VB.NET, I came across the default keyword, and I'm simply replacing it with nothing.

Is this the proper way to do it, or is there a better "translation" for that keyword?

Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • 1
    See http://stackoverflow.com/q/354136/48082 , especially the 1st question asked there. The answers are good and provide background. – Cheeso May 07 '11 at 17:28

2 Answers2

29

Yup, that's absolutely fine. While Nothing is usually meant to mean the equivalent of C#'s null, it can be used with value types to, to mean "the default value of that type".

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

"default" is used in the context of generics, especially when you do not know the type but still want to have an instance of the default value of the generic type. I am not very good at Visual Basic, but I can imagine there is an equivalent to do the same(?).

For an example of the usage of the default keyword in C#, see default Keyword in Generic Code (C# Programming Guide) (MSDN):

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Flawless
  • 127
  • 7
  • 2
    It doesn't *have* to be used with generics. You can just use (say) `default(string)` if you want. I would avoid using "instance" here - for example, `default(string)` is a null reference, rather than a reference to an instance of string. – Jon Skeet May 07 '11 at 17:36