What is the integer reference type in C#?

前端 未结 8 1080
日久生厌
日久生厌 2021-01-11 12:46

I\'d like to have an integer variable which can be set to null and don\'t want to have to use the int? myVariable syntax. I tried using int and

相关标签:
8条回答
  • 2021-01-11 13:33

    Yes you should use nullable types

    See Nullable

    Nullable<int> Nullable<float> 
    

    or simply

    int? float?
    

    PS:

    If you don't want to use ? notation or Nullable at all - simply use special structures for such a thing. For example DataTable:

    var table = new DataTable();
    table.Columns.Add('intCol',typeof(int));
    var row = table.NewRow();
    row['intCol'] = null; //
    
    0 讨论(0)
  • 2021-01-11 13:33

    Yes. That's why Microsoft added that. People were creating their own Nullable class so they included this pattern in .NET.

    0 讨论(0)
提交回复
热议问题