Ler en inglés

Compartir por


Nullable<T>.GetValueOrDefault Método

Definición

Recupera el valor del objeto Nullable<T> actual o un valor predeterminado.

Sobrecargas

GetValueOrDefault()

Recupera el valor del objeto Nullable<T> actual o el valor predeterminado del tipo subyacente.

GetValueOrDefault(T)

Recupera el valor del objeto Nullable<T> actual o el valor predeterminado especificado.

Ejemplos

En el ejemplo de código siguiente se recupera el valor de un Nullable<T> objeto si se define ese valor; de lo contrario, recupera el valor predeterminado o un valor predeterminado específico.

C#
// This code example demonstrates the
// Nullable<T>.GetValueOrDefault methods.

using System;

class Sample
{
    public static void Main()
    {
    float? mySingle = 12.34f;
    float? yourSingle = -1.0f;

   Console.WriteLine("*** Display a value or the default value ***\n");
// Display the values of mySingle and yourSingle.

    Display("A1", mySingle, yourSingle);

// Assign the value of mySingle to yourSingle, then display the values
// of mySingle and yourSingle. The yourSingle variable is assigned the
// value 12.34 because mySingle has a value.

    yourSingle = mySingle.GetValueOrDefault();
    Display("A2", mySingle, yourSingle);

// Assign null (Nothing in Visual Basic) to mySingle, which means no value is
// defined for mySingle. Then assign the value of mySingle to yourSingle and
// display the values of both variables. The default value of all binary zeroes
// is assigned to yourSingle because mySingle has no value.

    mySingle = null;
    yourSingle = mySingle.GetValueOrDefault();
    Display("A3", mySingle, yourSingle);

// Reassign the original values of mySingle and yourSingle.
    mySingle = 12.34f;
    yourSingle = -1.0f;

    Console.Write("\n*** Display a value or the ");
    Console.WriteLine("specified default value ***\n");

// Display the values of mySingle and yourSingle.
    Display("B1", mySingle, yourSingle);

// Assign the value of mySingle to yourSingle, then display the values
// of mySingle and yourSingle. The yourSingle variable is assigned the
// value 12.34 because mySingle has a value.

    yourSingle = mySingle.GetValueOrDefault(-222.22f);
    Display("B2", mySingle, yourSingle);

// Assign null (Nothing in Visual Basic) to mySingle, which means no value is
// defined for mySingle. Then assign the value of mySingle to yourSingle and
// display the values of both variables. The specified default value of -333.33
// is assigned to yourSingle because mySingle has no value.

    mySingle = null;
    yourSingle = mySingle.GetValueOrDefault(-333.33f);
    Display("B3", mySingle, yourSingle);
    }

// Display the values of two nullable of System.Single structures.
// The Console.WriteLine method automatically calls the ToString methods of
// each input argument to display its values. If no value is defined for a
// nullable type, the ToString method for that argument returns the empty
// string ("").
    public static void Display(string title, float? dspMySingle, float? dspYourSingle)
    {
    Console.WriteLine("{0}) mySingle = [{1}], yourSingle = [{2}]",
                      title, dspMySingle, dspYourSingle);
    }
}

/*
This code example produces the following results:

A1) mySingle = [12.34], yourSingle = [-1]
A2) mySingle = [12.34], yourSingle = [12.34]
A3) mySingle = [], yourSingle = [0]

*** Display a value or the specified default value ***

B1) mySingle = [12.34], yourSingle = [-1]
B2) mySingle = [12.34], yourSingle = [12.34]
B3) mySingle = [], yourSingle = [-333.33]

*/

GetValueOrDefault()

Source:
Nullable.cs
Source:
Nullable.cs
Source:
Nullable.cs

Recupera el valor del objeto Nullable<T> actual o el valor predeterminado del tipo subyacente.

C#
public T GetValueOrDefault ();
C#
public readonly T GetValueOrDefault ();

Devoluciones

T

Valor de la Value propiedad si la HasValue propiedad es true; de lo contrario, el valor predeterminado del tipo subyacente.

Comentarios

El GetValueOrDefault método devuelve un valor incluso si la HasValue propiedad es false (a diferencia de la Value propiedad , que produce una excepción). Si la HasValue propiedad es false, el método devuelve el valor predeterminado del tipo subyacente.

Consulte también

Se aplica a

.NET 9 e outras versións
Produto Versións
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

GetValueOrDefault(T)

Source:
Nullable.cs
Source:
Nullable.cs
Source:
Nullable.cs

Recupera el valor del objeto Nullable<T> actual o el valor predeterminado especificado.

C#
public T GetValueOrDefault (T defaultValue);
C#
public readonly T GetValueOrDefault (T defaultValue);

Parámetros

defaultValue
T

Valor que se devuelve si la propiedad HasValue es false.

Devoluciones

T

Valor de la propiedad Value si la propiedad HasValue es true; de lo contrario, el parámetro defaultValue.

Comentarios

El GetValueOrDefault método devuelve un valor incluso si la HasValue propiedad es false (a diferencia de la Value propiedad , que produce una excepción).

Consulte también

Se aplica a

.NET 9 e outras versións
Produto Versións
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0