[keyword] 預設關係字default(T) 輸出結果匯總

[keyword] 預設關係字default(T) 輸出結果匯總

default (C# Reference)

 

default Keyword in Generic Code (C# Programming Guide)

 

   1:          [TestMethod()]
   2:          public void DefaultTest()
   3:          {            
   4:              Assert.IsNull(default(VW_DW_EMP));
   5:   
   6:              Assert.IsNull(default(string));
   7:   
   8:              Assert.AreEqual(0, default(byte));
   9:   
  10:              Assert.AreEqual(0, default(int));
  11:   
  12:              Assert.AreEqual(0, default(long));
  13:   
  14:              Assert.AreEqual(0, default(float));
  15:              
  16:              Assert.AreEqual(0, default(double));
  17:   
  18:              Assert.AreEqual(0, default(decimal));
  19:              
  20:              Assert.AreEqual(0, default(char));
  21:   
  22:              Assert.AreEqual(false, default(bool));
  23:   
  24:              Assert.AreEqual(DateTime.MinValue, default(DateTime));
  25:   
  26:              Assert.AreEqual(ENUM_LAB.NONE, default(ENUM_LAB));
  27:   
  28:              Assert.AreEqual(new STRUCT_LAB(), default(STRUCT_LAB));
  29:   
  30:              Assert.IsNull(default(int?));
  31:          }
  32:   
  33:          enum ENUM_LAB
  34:          {
  35:              NONE, UT01, UT02
  36:          }
  37:   
  38:          struct STRUCT_LAB
  39:          {
  40:              string UT;
  41:          }

 

同理可證 LINQ :

   1:          [TestMethod()]
   2:          public void LinqDefaultTest()
   3:          {
   4:              int[] intArray = { };
   5:              Assert.AreEqual(0, intArray.FirstOrDefault());
   6:   
   7:              string[] strArray = { };
   8:              Assert.IsNull(strArray.FirstOrDefault());
   9:   
  10:              VW_DW_EMP[] empArray = { };
  11:              Assert.IsNull(empArray.FirstOrDefault());
  12:   
  13:              bool[] boolArray = { };
  14:              Assert.AreEqual(false, boolArray.FirstOrDefault());
  15:   
  16:              bool?[] nullableBoolArray = { };
  17:              Assert.IsNull(nullableBoolArray.FirstOrDefault());
  18:   
  19:              DateTime[] dateTimeArray = { };
  20:              Assert.AreEqual(DateTime.MinValue, dateTimeArray.FirstOrDefault());
  21:          }