zoukankan      html  css  js  c++  java
  • C# Nullable 类型

    Nullable types are one of those great little inventions to help you with the impedance mismatch between object-oriented applications and relational databases.  Let's take a fake Order Class that maps to an Orders Table in your database.  The class might look like this:

     

    Order Class
    using System;
    using System.Collections;
    
    public class Order
    {
        private int _id;
        private string _number = string.Empty;
        private decimal _amount;
        private DateTime _shipDate;
        
        public int ID
        {
            get { return _id;; }
        }
        
        public string Number
        {
            get { return _number; }
            set { _number = value; }
        }
        
        public decimal Amount
        {
            get { return _amount; }
            set { _amount = value; }
        }
        
        public DateTime ShipDate
        {
            get { return _shipDate; }
            set { _shipDate = value; }
        }
        
        public Order() {}
    }

    Now if an order has not shipped, the ShipDate for the order will be null in the Orders Table and you would expect the ShipDate to be null (Nothing in VB) in the Order object as well.  However, DateTime is a struct (value type) and cannot be null.  If not assigned, ShipDate will default to System.DateTime.MinValue.  This is the mismatch.

    We can get around this in C# 2.0 by making _shipDate a nullable DateTime type.  You can make it a nullable type by declaring it either:

    • Nullable<DateTime> _shipDate;
    • DateTime? _shipDate;

    with the Nullable structure being defined as follows:

     

    Nullable Types
    struct Nullable<T>
    {
        public bool HasValue;
        public T Value;
    }

    Now the new class is defined as follows:

     

    Order Class With Nullable ShipDate
    using System;
    using System.Collections;
    
    public class Order
    {
        private int _id;
        private string _number = string.Empty;
        private decimal _amount;
        private DateTime? _shipDate;
        
        public int ID
        {
            get { return _id;; }
        }
        
        public string Number
        {
            get { return _number; }
            set { _number = value; }
        }
        
        public decimal Amount
        {
            get { return _amount; }
            set { _amount = value; }
        }
        
        public DateTime? ShipDate
        {
            get { return _shipDate; }
            set { _shipDate = value; }
        }
        
        public Order() {}
    }

    Checking For Null

    If _shipDate is not assigned now, it will have a null value and you can determine if it has been assigned by the following statements:

    if (order.ShipDate != null) ...

    or

    if (order.ShipDate.HasValue) ...

     

    InvalidOperationException

    If _shipDate has not been assigned and you try to extract the DateTime value from it, you will get an InvalidOperationException:

    DateTime shipDate = Order.ShipDate.Value; // throws exception if not assigned

     

    C# Nullable Types will play a big role in defining business objects and data transfer objects and how they map to relational databases. 

  • 相关阅读:
    Atitit opencv3.0  3.1 3.2 新特性attilax总结
    Atitit html5.1 新特性attilax总结
    Atitit http2 新特性
    Atitit 大龄软件工程师的出路attilax总结
    Atitit 软件项目系统托盘图标解决方案
    Atitit js canvas的图像处理类库attilax总结与事业
    Atitit 切入一个领域的方法总结 attilax这里,机器学习为例子
    css知多少(8)——float上篇
    css知多少(7)——盒子模型
    css知多少(6)——选择器的优先级
  • 原文地址:https://www.cnblogs.com/weekend001/p/1657519.html
Copyright © 2011-2022 走看看