Nullable<T>.GetValueOrDefault 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
현재 Nullable<T> 개체의 값이나 기본값을 검색합니다.
오버로드
GetValueOrDefault() |
현재 Nullable<T> 개체의 값 또는 기본 유형의 기본값을 검색합니다. |
GetValueOrDefault(T) |
현재 Nullable<T> 개체의 값이나 지정된 기본값을 검색합니다. |
예제
다음 코드 예제에서는 해당 값이 정의된 경우 개체의 Nullable<T> 값을 검색하고, 그렇지 않으면 기본값 또는 특정 기본값을 검색합니다.
// 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]
*/
// This code example demonstrates the
// Nullable<T>.GetValueOrDefault methods.
open System
// Display the values of two nullable of System.Single structures.
// The printfn string interpolation 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 ("").
let display title dspMySingle dspYourSingle =
printfn $"{title}) mySingle = [{dspMySingle}], yourSingle = [{dspYourSingle}]"
let mySingle = Nullable 12.34f
let yourSingle = Nullable -1f
[<EntryPoint>]
let main _ =
printfn "*** Display a value or the default value ***\n"
// Display the values of mySingle and yourSingle.
display "A1" mySingle yourSingle
// Shadow 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.
let yourSingle = mySingle.GetValueOrDefault()
display "A2" mySingle yourSingle
// Shadow 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.
let mySingle = Nullable()
let yourSingle = mySingle.GetValueOrDefault()
display "A3" mySingle yourSingle
// Shadow the original values of mySingle and yourSingle.
let mySingle = Nullable 12.34f
let yourSingle = Nullable -1.0f
printf "\n*** Display a value or the "
printfn "specified default value ***\n"
// Display the values of mySingle and yourSingle.
display "B1" mySingle yourSingle
// Shadow 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.
let yourSingle = mySingle.GetValueOrDefault -222.22f
display "B2" mySingle yourSingle
// Shadow null (Nothing in Visual Basic) to mySingle, which means no value is
// defined for mySingle. Then shadow 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.
let mySingle = Nullable()
let yourSingle = mySingle.GetValueOrDefault -333.33f
display "B3" mySingle yourSingle
0
// 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]
' This code example demonstrates the
' Nullable(Of T).GetValueOrDefault methods.
Class Sample
Public Shared Sub Main()
Dim mySingle As Nullable(Of System.Single) = 12.34F
Dim yourSingle As Nullable(Of System.Single) = - 1.0F
Console.WriteLine("*** Display a value or the default value ***" & vbCrLf)
' 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 = Nothing
yourSingle = mySingle.GetValueOrDefault()
Display("A3", mySingle, yourSingle)
' Reassign the original values of mySingle and yourSingle.
mySingle = 12.34F
yourSingle = - 1.0F
Console.Write(vbCrLf & "*** Display a value or the ")
Console.WriteLine("specified default value ***" & vbCrLf)
' 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 = Nothing
yourSingle = mySingle.GetValueOrDefault(- 333.33F)
Display("B3", mySingle, yourSingle)
End Sub
' 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 Shared Sub Display(ByVal title As String, _
ByVal dspMySingle As Nullable(Of System.Single), _
ByVal dspYourSingle As Nullable(Of System.Single))
If (True) Then
Console.WriteLine("{0}) mySingle = [{1}], yourSingle = [{2}]", _
title, dspMySingle, dspYourSingle)
End If
End Sub
End Class
'
'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
현재 Nullable<T> 개체의 값 또는 기본 유형의 기본값을 검색합니다.
public:
T GetValueOrDefault();
public T GetValueOrDefault ();
public readonly T GetValueOrDefault ();
member this.GetValueOrDefault : unit -> 'T
Public Function GetValueOrDefault () As T
반환
속성이 Value 이면 HasValue 속성의 값이고 true
, 그렇지 않으면 기본 형식의 기본값입니다.
설명
메서드는 GetValueOrDefault 속성 false
이 이더라도 HasValue 값을 반환합니다(예외를 Value throw하는 속성과 달리). 속성이 이 HasValuefalse
면 메서드는 기본 형식의 기본 값을 반환합니다.
추가 정보
적용 대상
GetValueOrDefault(T)
- Source:
- Nullable.cs
- Source:
- Nullable.cs
- Source:
- Nullable.cs
현재 Nullable<T> 개체의 값이나 지정된 기본값을 검색합니다.
public:
T GetValueOrDefault(T defaultValue);
public T GetValueOrDefault (T defaultValue);
public readonly T GetValueOrDefault (T defaultValue);
member this.GetValueOrDefault : 'T -> 'T
Public Function GetValueOrDefault (defaultValue As T) As T
매개 변수
- defaultValue
- T
HasValue 속성이 false
인 경우 반환할 값입니다.
반환
Value 속성이 true
이면 HasValue 속성의 값이고, 그렇지 않으면 defaultValue
매개 변수입니다.
설명
메서드는 GetValueOrDefault 속성 false
이 이더라도 HasValue 값을 반환합니다(예외를 Value throw하는 속성과 달리).
추가 정보
적용 대상
.NET