Is the default (TSource) a real method?

The MSDN help page on Enumerable.FirstOrDefault Method has the result of the method explained as:

default (TSource) if the source is empty; otherwise, the first element is in the source.

The notes section contains a note:

The default for reference and null types is null.

I've always done a check for the value null

(VB.NET:) Nothing

, but is there any one default(TSource)

that can be used instead of null/Nothing

literal? (For example default(int)

.)

I cannot find the method default(TSource)

, but it is listed on the help page. Or is it not a method?

EDIT: default(TSource)

appears on the MSDN page for both C # and VB and I'm interested in answering both languages.

+3


source to share


3 answers


I've always done a check for null (VB.NET: Nothing)

This is not entirely correct. Nothing

does not match with null

; Nothing

means null

when assigning or matching to a nullable type (reference type or Nullable<T>

) with =

or when comparing with Is Nothing

and means the default value for the non-nullable type to which it is assigned or compared to =

.

Hence VB:

Dim b as Boolean = 0 = Nothing ' b is True

      

is not the same as C #:

bool b = 0 == null; // b is false

      

but rather:



bool b = 0 == default(int); // b is true

      

So the VB.NET equivalent is default(T)

valid Nothing

when not being compared to use Is

.

In VB.NET you are not allowed to do val Is Nothing

with val

not equal to NULL, and in C # you can do val == null

, but it raises a warning (and always does false

).

In VB.NET, you are allowed to do val Is Nothing

with a generic type, which can be null, as well as C # and val == null

, in which case the check is that val

the type is NULL and that it is null (and no waste in this case, generally, in the case of a null-valued type, jitter optimizes whatever happens if val == null

/ val Is Nothing

, since it knows it can never happen).

The following VB.NET and C # methods are equivalent:

public static bool Demonstrate<T>(T x)
{
  T y = default(T);
  bool isNull = x == null;
  bool isDefault = x.Equals(default(T));
  int zero = default(int)
  return zero == default(int);
}

Public Shared Function Demonstrate(Of T)(x As T) As Boolean
  Dim y As T = Nothing
  Dim isNull As Boolean = x Is Nothing
  Dim isDefault As Boolean = x.Equals(Nothing)
  Dim zero As Integer = Nothing
  Return zero = Nothing
End Function

      

+2


source


You can use the operator default

which is documented here . It returns null

for reference types and a type-dependent default for value types.



+3


source


default is a keyword used to get the default value Type

. TSource

is a generic parameter. It's just a place for the type marker.

When a method is called with IEnumerable<int>

, TSource

will int

, TSource

will String

when called with, IEnumerable<String>

and so on. Given what default(TSource)

becomes default(int)

in this case (called with int

).

There is nothing like this default(TSource)

if there is no type TSource

or a generic parameter named TSource

.

+3


source







All Articles