Nullable ジェネリック構造体とは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Nullable ジェネリック構造体の意味・解説 

Nullable ジェネリック構造体

メモ : この構造体は、.NET Framework version 2.0新しく追加されたものです。

基になる型が値型オブジェクト表します参照型のように null 参照 (Visual Basic では Nothing) も割り当てることができます

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
Public Structure Nullable(Of
 T As Structure)
Dim instance As Nullable(Of
 T)
[SerializableAttribute] 
public struct Nullable<T> where T : struct
[SerializableAttribute] 
generic<typename T> where T : ValueType
public value class Nullable
J# では、ジェネリックな型およびメソッド使用できますが、新規に宣言することはできません。
JScript では、ジェネリックな型およびメソッド使用できません。
解説解説

型に値または null 参照 (Visual Basic では Nothing) を割り当てることができる場合、その型を NULL 許容型呼びますnull は、型に値がないことを表します。そのため、NULL 許容型では、値を表すことも、値がないことを表すこともできます。たとえば、String のような参照型は、NULL 許容型です。Int32 のような値型は、NULL 許容型ではありません。値型は、その型に適した値を表すだけの容量しかなく、null 値を表すための追加容量がないので、NULL 許容型ではありません。

設計上、参照型NULL 許容型なので、Nullable 構造体は、NULL 許容型としての値型使用のみをサポートします

Nullable クラスは、Nullable 構造体への補完的なサポート提供しますNullable クラスは、NULL 許容型の基になる型の取得、および基になる値型ジェネリック型比較演算等値演算サポートしない NULL 許容型ペア対す比較演算等値演算サポートします

シナリオ

状況によって値の有無決定される場合、この値を表すために NULL 許容型使用します。たとえば、HTML タグオプション属性特定のタグ存在し別のタグには存在しない場合や、データベース テーブルnull 許容列が同じテーブル内の特定の行に存在し別の行には存在しない場合などです。

属性または列をクラス内のフィールドとして表し、そのフィールド値型として定義できます。このフィールドには、属性または列を表すすべての有効な値を格納できますが、属性または列が存在しないことを表す追加の値を格納できません。この場合は、フィールド値型ではなくNULL 許容型として定義します

基本的なプロパティ
ボックス化ボックス化解除
使用例使用例

Microsoft Pubs サンプル データベーステーブル内の 3 つの行を定義するコード例次に示します。このテーブルには、null許容列が 2 列と null 許容列が 2 列含まれます。

' This code example demonstrates the Nullable(Of T) class.
' The code example defines a database table in which two columns 
' are nullable. In the application, an array of rows is created 
' and initialized. The table rows could subsequently be 
' written to a database.

Imports System

Class Sample
    ' Define the "titleAuthor" table of the Microsoft "pubs"
 database. 
    
    Public Structure titleAuthor
    ' Author ID; format ###-##-####
        Public au_id As String
    ' Title ID; format AA####
        Public title_id As String
    ' Author ORD is nullable.
        Public au_ord As Nullable(Of
 Short)
    ' Royalty Percent is nullable.
        Public royaltyper As Nullable(Of
 Integer)
    End Structure 'titleAuthor
    
    Public Shared Sub Main()
 
    ' Declare and initialize the titleAuthor array.
        Dim ta(2) As titleAuthor
        ta(0).au_id = "712-32-1176"
        ta(0).title_id = "PS3333"
        ta(0).au_ord = 1
        ta(0).royaltyper = 100
        
        ta(1).au_id = "213-46-8915"
        ta(1).title_id = "BU1032"
        ta(1).au_ord = Nothing
        ta(1).royaltyper = Nothing
        
        ta(2).au_id = "672-71-3249"
        ta(2).title_id = "TC7777"
        ta(2).au_ord = Nothing
        ta(2).royaltyper = 40
        
    ' Display the values of the titleAuthor array elements, and 
    ' display a legend.
        Display("Title Authors Table", ta)
        Console.WriteLine("Legend:")
        Console.WriteLine("An Author ORD of -1 means no value
 is defined.")
        Console.WriteLine("A Royalty % of 0 means no value is
 defined.")
    End Sub 'Main
    
    ' Display the values of the titleAuthor array elements.
    Public Shared Sub Display(ByVal
 dspTitle As String, _
                              ByVal dspAllTitleAuthors() As
 titleAuthor) 
        Console.WriteLine("*** {0} ***", dspTitle)
        Dim dspTA As titleAuthor
        For Each dspTA In
 dspAllTitleAuthors
            Console.WriteLine("Author ID ... {0}",
 dspTA.au_id)
            Console.WriteLine("Title ID .... {0}",
 dspTA.title_id)
            Console.WriteLine("Author ORD .. {0}",
 dspTA.au_ord.GetValueOrDefault(-1))
            Console.WriteLine("Royalty % ... {0}",
 dspTA.royaltyper.GetValueOrDefault(0))
            Console.WriteLine()
        Next dspTA
    End Sub 'Display
End Class 'Sample

'
'This code example produces the following results:
'
'*** Title Authors Table ***
'Author ID ... 712-32-1176
'Title ID .... PS3333
'Author ORD .. 1
'Royalty % ... 100
'
'Author ID ... 213-46-8915
'Title ID .... BU1032
'Author ORD .. -1
'Royalty % ... 0
'
'Author ID ... 672-71-3249
'Title ID .... TC7777
'Author ORD .. -1
'Royalty % ... 40
'
'Legend:
'An Author ORD of -1 means no value is defined.
'A Royalty % of 0 means no value is defined.
'
// This code example demonstrates the Nullable<T> class.
// The code example defines a database table in which two columns 
// are nullable. In the application, an array of rows is created 
// and initialized. The table rows could subsequently be 
// written to a database.

using System;

class Sample 
{
// Define the "titleAuthor" table of the Microsoft "pubs"
 database. 
    public struct titleAuthor 
    {
    // Author ID; format ###-##-####
    public string au_id;
    // Title ID; format AA####
    public string title_id;
    // Author ORD is nullable.
    public short? au_ord;
    // Royalty Percent is nullable.
    public int? royaltyper;
    }

    public static void Main()
 
    {
// Declare and initialize the titleAuthor array.
    titleAuthor[] ta = new titleAuthor[3];
    ta[0].au_id = "712-32-1176";
    ta[0].title_id = "PS3333";
    ta[0].au_ord = 1;
    ta[0].royaltyper = 100;
  
    ta[1].au_id = "213-46-8915";
    ta[1].title_id = "BU1032";
    ta[1].au_ord = null;
    ta[1].royaltyper = null;

    ta[2].au_id = "672-71-3249";
    ta[2].title_id = "TC7777";
    ta[2].au_ord = null;
    ta[2].royaltyper = 40;

// Display the values of the titleAuthor array elements, and 
// display a legend.
    Display("Title Authors Table", ta);
    Console.WriteLine("Legend:");
    Console.WriteLine("An Author ORD of -1 means no value is defined.");
    Console.WriteLine("A Royalty % of 0 means no value is defined.");
    }

// Display the values of the titleAuthor array elements.
    public static void Display(string
 dspTitle, 
                               titleAuthor[] dspAllTitleAuthors)
    {
    Console.WriteLine("*** {0} ***", dspTitle);
    foreach (titleAuthor dspTA in dspAllTitleAuthors)
       {
       Console.WriteLine("Author ID ... {0}", dspTA.au_id);
       Console.WriteLine("Title ID .... {0}", dspTA.title_id);
       Console.WriteLine("Author ORD .. {0}", dspTA.au_ord ?? -1);
       Console.WriteLine("Royalty % ... {0}", dspTA.royaltyper ?? 0);
       Console.WriteLine();       
       }
    }
}

/*
This code example produces the following results:

*** Title Authors Table ***
Author ID ... 712-32-1176
Title ID .... PS3333
Author ORD .. 1
Royalty % ... 100

Author ID ... 213-46-8915
Title ID .... BU1032
Author ORD .. -1
Royalty % ... 0

Author ID ... 672-71-3249
Title ID .... TC7777
Author ORD .. -1
Royalty % ... 40

Legend:
An Author ORD of -1 means no value is defined.
A Royalty % of 0 means no value is defined.

*/
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からNullable ジェネリック構造体を検索した結果を表示しています。
Weblioに収録されているすべての辞書からNullable ジェネリック構造体を検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からNullable ジェネリック構造体 を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Nullable ジェネリック構造体」の関連用語

Nullable ジェネリック構造体のお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Nullable ジェネリック構造体のページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS