.NET(C#)可空类型用法及示例

.NET(C#)可空类型用法

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <asp:TextBox ID="txtLength" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtWidth" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtHeight" runat="server"></asp:TextBox><br />
        <br />
        <asp:TextBox ID="txtDtMdf" runat="server"></asp:TextBox><br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>
</body>
</html>

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {         

        string s1 = null;
        string s2 = string.Empty;
        string s3 = "";

        //Response.Write("<script language='javascript' defer>alert('" + Convert.ToString(null) + "');</script>");


        if (s1 == s2)
        {
            Response.Write("s1 == s2"+"<br />");
        }
        else
        {
            Response.Write("s1 <> s2"+"<br />");
        }

        if (s2 == s3)
        {
            Response.Write("s2 == s3" + "<br />");
        }
        else
        {
            Response.Write("s2 <> s3" + "<br />");
        }

        if (s1 == s3)
        {
            Response.Write("s1 == s3" + "<br />");
        }
        else
        {
            Response.Write("s1 <> s3" + "<br />");
        }

       
        //decimal? length=null;
        //if (txtLength.Text.Trim() != string.Empty)
        //    length = decimal.Parse(txtLength.Text.Trim());

        //decimal? length;
        //if (txtLength.Text.Trim() == string.Empty)
        //    length = null;
        //else
        //    length = decimal.Parse(txtLength.Text.Trim());

 

 

        //编译: 错误 1 无法将 NULL 转换成“decimal”,因为它是一种值类型
        //decimal d1 = null;

 

        //decimal? 等价于 System.Nullable<decimal>

        //int? 等价于 System.Nullable<int>

        //bool? 等价于 System.Nullable<bool>

 

        decimal? d2 = null;
        decimal? d3 = (decimal?)null;  

 

        //编译: 错误 1 无法确定条件表达式的类型,因为“<null>”和“decimal”之间没有隐式转换

        //decimal? length = (txtLength.Text.Trim() == string.Empty ? null : decimal.Parse(txtLength.Text.Trim()));

 

        decimal? length = (txtLength.Text.Trim() == string.Empty ? (decimal?)null : decimal.Parse(txtLength.Text.Trim()));
        decimal? width = (txtWidth.Text.Trim() == string.Empty ? (decimal?)null : decimal.Parse(txtWidth.Text.Trim()));       
        decimal? height = (txtHeight.Text.Trim() == string.Empty ? (decimal?)null : decimal.Parse(txtHeight.Text.Trim()));

 

 

        /*
        //int? i1 = null;
        //int i2 = i1 ?? 0;

        //编译: 错误 1 无法将类型“string”隐式转换为“decimal?”
        decimal? height = txtHeight.Text.Trim() ?? null;

 

        //编译: 错误 1 无法将类型“string”转换为“decimal?” 
        decimal? height = (decimal?)txtHeight.Text.Trim() ?? null;

 

        //编译: 错误 1 运算符“??”无法应用于“decimal”和“<null>”类型的操作数 
        decimal? height = decimal.Parse(txtHeight.Text.Trim()) ?? null;    

 

        //编译: ========== 生成: 1 成功或最新,0 失败,0 被跳过 ==========   
        decimal? height = (decimal?)decimal.Parse(txtHeight.Text.Trim()) ?? null;
        //运行: 当height为空时: System.FormatException: 输入字符串的格式不正确。
        */

 

 

        DateTime dtAdd = DateTime.Now;
        DateTime? dtMdf = (txtDtMdf.Text.Trim() == string.Empty ? (DateTime?)null : DateTime.Parse(txtDtMdf.Text.Trim()));

        Response.Write("length:" +length.ToString()+"<BR />");
        Response.Write("width:" + width.ToString() + "<BR />");
        Response.Write("height:" + height.ToString() + "<BR />");       
        Response.Write("dtAdd:" + dtAdd.ToString() + "<BR />");
        Response.Write("dtMdf:" + dtMdf.ToString() + "<BR />");
    }
}

 

 

版权所有,转载请务必保留以下信息,否则将追究法律责任!!!

作者主页 http://blog.csdn.net/cadenza7

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值