private void InsertRawDataFile(RawDataFile obj) { System.Nullable <int> p1 = obj.FileId; System.Nullable <System.DateTime> p2 = obj.UploadDate; string p3 = obj.UploaderLogin; this.RawDataFile_Insert(obj.FileName, obj.FolderName, obj.Contents, ref p1, ref p2, ref p3); obj.FileId = p1.GetValueOrDefault(); obj.UploadDate = p2.GetValueOrDefault(); obj.UploaderLogin = p3; }
public static bool IsEmpty(System.Nullable <decimal> value) { if (value.HasValue) { System.Nullable <decimal> nullable = value; if (nullable.GetValueOrDefault() == (0M)) { return(nullable.HasValue); } return(false); } return(true); }
public ResumenEmpleados GetResumenEmpleados(String oficio) { List <EMP> empleados = this.GetEmpleadosOficio(oficio); int personas = empleados.Count; int? maximo = empleados.Max(x => x.SALARIO); System.Nullable <int> suma = empleados.Sum(z => z.SALARIO); double? media = empleados.Average(z => z.SALARIO); ResumenEmpleados resumen = new ResumenEmpleados(); resumen.Personas = personas; resumen.SumaSalarial = suma.GetValueOrDefault(); resumen.MaximoSalario = maximo.GetValueOrDefault(); resumen.MediaSalarial = media.GetValueOrDefault(); return(resumen); }
static void Main(string[] args) { var numbers = new GenericList <int>(); numbers.Add(10); //int a = numbers[0]; var dictionary = new GenericDictionary <string, int>(); var num = new Nullable <int>(5); Console.WriteLine("has Value ? " + num.HasValue); Console.WriteLine("Value: " + num.GetValueOrDefault()); var num1 = new System.Nullable <int>(5); Console.WriteLine("has Value ? " + num1.HasValue); Console.WriteLine("Value: " + num1.GetValueOrDefault()); }
static void Main(string[] args) { // 隱式轉換,小的轉大的,可以自動轉換 int i = 10; long l = i; C1 c1 = new C2(); // 隱式轉換,向上轉型 // 顯式轉換,大的轉小的,必須強制轉換 double d = 10.05; int iFormD = (int)d; Console.WriteLine(iFormD); // 輸出 10,資料會有缺失的風險,以及例外錯誤 C1 c11 = new C1(); try { C2 c2 = (C2)c11; // 顯式轉換,向下轉換,執行可能會出現例外錯誤 } catch (Exception e) { Console.WriteLine(e.Message); // throw e; } // 透過 is 判斷型別 Console.WriteLine(c11 is C1); // 輸出 True Console.WriteLine(c11 is C2); // 輸出 False // 利用 as,安全的方式進行轉換 C2 c22 = c11 as C2; // 可運行不會出錯,c11在轉換的過程中發現不能轉為 C2 型別,會丟回一個 null 值 Console.WriteLine(c22 == null); // 輸出 True // int iAS = 5 as int; // as 只能用於參考型別,因為實值型別,不能給予 null 值。 string sFromi = i.ToString(); // ToString() 轉換成字串 int iFromS = Convert.ToInt32("100"); // Convert 轉換型別 //int iFromS2 = Convert.ToInt32("CSharp"); // 錯誤:輸入字串格式不正確。 int iFromS3 = Int32.Parse("101"); // Parse 轉換型別 //int iFromS4 = Int32.Parse("CSharp"); // 錯誤:輸入字串格式不正確。 int iFromS5; bool succeed = Int32.TryParse("102", out iFromS5); // TryParse 試著去轉換 Console.WriteLine(succeed + " : " + iFromS5); // 輸出 True : 102,轉換成功 bool succeed2 = Int32.TryParse("CSharp", out iFromS5); Console.WriteLine(succeed2 + " : " + iFromS5); // 輸出 False : 0,轉換失敗,輸出整數的空值 0 // 實作自己的轉換,透過 IConvertible, TypeConverter int iToBoxing = 100; object iBoxed = iToBoxing; // boxing 隱式轉換,因為所有的型別都是繼承 System.object,向上轉換 int iUnBoxing = (int)iBoxed; // unBoxing 顯式轉換,由父類別轉為子類別,向下轉型 object nullObject = null; // int iNull = (int)nullObject; // 錯誤:NullReferenceException。 int?iNullable = null; // 宣告一個可為 null 值的 int System.Nullable <int> iNullable2 = 100; // 等同於 int? Console.WriteLine(iNullable2.Value); // 輸出 100 // Console.WriteLine(iNullable.Value); // 錯誤:可為 Null 的物件必須具有值 Console.WriteLine(iNullable.HasValue); // 輸出:false,可以預先判斷 Console.WriteLine(iNullable.GetValueOrDefault()); // 輸出:0,若為空值,給予預設的 null 值 Console.WriteLine(iNullable2.GetValueOrDefault()); // 輸出:100 int i2 = iNullable ?? 500; // 若 ?? 前的變數值為 null,那就給予 ?? 後面的值 Console.WriteLine(i2); // 輸出:500 int i3 = iNullable2 ?? 500; // 若 ?? 前的變數值為 null,那就給予 ?? 後面的值 Console.WriteLine(i3); // 輸出 100 }
static void Main(string[] args) { //隐式转换 int i = 10; long l = i; C1 c1 = new C2(); //显示转换 double d = 10.05; int iFromD = (int)d; Console.WriteLine(iFromD); C1 c11 = new C1(); //C2 c2 = (C2)c11; try { C2 c2 = (C2)c11; } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(c11 is C1); //true Console.WriteLine(c11 is C2); //false C2 c22 = c11 as C2; //编译成功,运行成功,无法转换时,返回null. Console.WriteLine(c22 == null); //true,降低消耗,只能用于非null类型或引用类型,比如值类型,就不是非null类型 //int iAs = "s" as int; //错误 1 as 运算符必须用于引用类型或可以为 null 的类型(“int”是不可以为 null 值的类型) E:\极客学院2\ASP.NET工程师\1.初级ASP.NET工程师\2.C#语言基础\4.C#的类型转换\Lession5TypeConvertion\Lession5TypeConvertion\Program.cs 38 23 Lession5TypeConvertion Console.WriteLine("------------------------"); string sFrom1 = i.ToString(); int iFroms = Convert.ToInt32("10"); //int iFroms2 = Convert.ToInt32("zilong");//System.FormatException int iFrom3 = Int32.Parse("101"); //int iFrom4 = Int32.Parse("zilong");//System.FormatException int iFrom5; bool succeed = Int32.TryParse("zilong", out iFrom5); Console.WriteLine(iFrom5);//0 //IConvertible,TypeConverter Console.WriteLine("------------------------"); int iToBoxing = 100; object iBoxed = iToBoxing; int iUnBoxing = (int)iBoxed; object nullOjbect = iFrom5; int iNUll = (int)nullOjbect; int?iNUllable = null; // = null; System.Nullable <int> iNUllable2 = 100; //和上一句等价 //Console.WriteLine(iNUllable.Value); Console.WriteLine(iNUllable.HasValue); Console.WriteLine(iNUllable.GetValueOrDefault()); Console.WriteLine(iNUllable2.GetValueOrDefault()); int iii = iNUllable ?? 999; int iiii = iNUllable2 ?? 999; Console.WriteLine(iii); Console.WriteLine(iiii); Console.ReadLine(); }