在C#中"??"是什么意思?

程序猿GG 2009-03-23 10:29:20
我在MSDN上看到如下一段: (http://msdn.microsoft.com/zh-cn/library/bb386454.aspx)
下面的示例显示用于母版页上的 ScriptManager 控件和内容页上的 UpdatePanel 控件的标记。在本示例中,将在母版页上定义一个名为 LastUpdate 的属性,并从 UpdatePanel 控件内部引用该属性。
<%@ Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

public DateTime LastUpdate
{
get
{
return (DateTime)(ViewState["LastUpdate"] ?? DateTime.Now);
}
set
{
ViewState["LastUpdate"] = value;
}
}


protected void MasterButton2_Click(object sender, EventArgs e)
{
LastUpdate = DateTime.Now;
((UpdatePanel)ContentPlaceHolder1.FindControl("UpdatePanel1")).Update();

}

protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterAsyncPostBackControl(Button2);
}
</script>

<html >
<head id="Head1" runat="server">
<title>ScriptManager in Master Page Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Panel ID="MasterPanel1" runat="server" GroupingText="Master Page">
<asp:Button ID="Button1" runat="server" Text="Full Page Refresh" />
<asp:Button ID="Button2" runat="server" Text="Refresh Panel" OnClick="MasterButton2_Click" />
</asp:Panel>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>


上面代码中, 我标记为红色字体的是什么意思呀..
...全文
1449 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
大月球 2011-07-24
  • 打赏
  • 举报
回复
今天涨了见识了,想再问问啊,??是不是类似与三元运算啊。a=b??“-1”。这样格式是正确吗?双问号后面值不用逗号隔开??
草山丝 2011-07-22
  • 打赏
  • 举报
回复
I  do not know.
ojekleen 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 liujiayu10 的回复:]
学习了
[/Quote]
zzxap 2009-03-23
  • 打赏
  • 举报
回复
学习了
kkun_3yue3 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kkun_3yue3 的回复:]
只能在引用类型中使用,值类型永远不为null
如int,double,decimal,DateTime,float等
[/Quote]
sorry 可空类型除外
可空类型如这样定义的"值类型" int? i = null;
liujiayu10 2009-03-23
  • 打赏
  • 举报
回复
学习了
yanyun54321 2009-03-23
  • 打赏
  • 举报
回复
我是猎头公司的angel,目前上海的一家港资公司有CTO的职位需求,有兴趣者请联系我
msn:angel_lu{at}live.com
email: angel{at}jingjie-wisemen.com
要求如下
职责描述:
1. 负责公司项目管理系统的架构、设计、开发和质量管理工作;
2. 负责组建一支优秀的开发团队并维护团队处于健康状态;
3. 熟悉行业领域软件开发采用的技术,把握公司产品的发展方向,全面监控产品开发质量、进度和成本控制;
4. 针对公司具体项目实施,控制项目需求变更,支持项目实施的顺利进行;
5. 负责技术中心各部门的日常管理工作和部门建设,制订并监督执行技术部门工作计划;
6. 负责技术指导工作,在项目的技术管理和人员管理方面能够做出总体规划;
7. 参与制定公司软件产品的发展方向,确定公司开发采用的技术和产品框架结构及实施计划,规划产品研发进度安排,根据公司需要确定产品开发周期及人员安排;
8. 负责开发过程中阶段和关键工件的评审工作;
9. 负责公司技术管理文件的评审工作;
10.负责产品的内部验收确认工作;
11.负责知识库的建立和管理工作;
12.负责公司的技术创新工作,以保证公司在行业技术上的领先;

教育要求
计算机或相关专业本科以上学历
专业经验要求
1. 具有八年以上系统开发设计经验,其中五年以上的技术管理经理以上职务经验,有CTO经验者优先考虑;
2. 曾主持或参与项目管理类系统的设计及平台开发;
3. 对目前成熟应用的开发技术及框架有深入了解及把握;
4. 熟悉SOA技术,对设计模式等有深入了解,负责过大型系统设计工作
5. 丰富的开发团队管理经验,精通项目管理方法和项目实施流程
6. 从事过策划、设计或开发运营级应用软件产品的经验者优先考虑。
7. 有资深的技术、管理、项目实施和售前支持经验;
8. 对软件企业的生产管理有丰富的经验,对公司级软件的规范化管理有成功的案例
专业技能要求
1、 有大型应用系统的分析及设计能力;
2、 有建筑行业信息化建设项目实施经验者优先;
3、 对设计模式有一定的研究,对常见的设计模式比较熟;
4、 具有优秀的系统架构、编程和调试技能;
5、 熟悉软件质量管理标准/方法、工具 (如., WTT, Scenario Builder, etc);
6、 熟悉 C#, ASP.Net/ASP,XML, SQL Server, OOA/OOD, Web Services;
其他要求
1. 具有良好的英语沟通能力,尤其是英文读写;
2. 良好的学习能力和解决问题的能力;
3. 身体健康,性格沉稳,能承受较大的精神和心理压力;
4. 户口不限
wuyi8808 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zzxap 的回复:]
写错了吧。应该是三元表达式
ViewState["LastUpdate"]=2009-02-03?2009-02-03:DateTime.Now
[/Quote]

另外,这里的 = 应该是 ==

C# 中 = 是赋值运算符, == 是比较运算符。

且不说后面的 2009-02-03 不能直接这么写,C# 中没有这种日期常量。
cja03 2009-03-23
  • 打赏
  • 举报
回复
三目运算的简写(针对null)

string a=null;
string b= a==null?"-1":a;
----->
string b= a??"-1";
terry 2009-03-23
  • 打赏
  • 举报
回复
学习。
wuyi8808 2009-03-23
  • 打赏
  • 举报
回复
a ?? b

相当于:

a != null ? a : b
wuyi8808 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zzxap 的回复:]
写错了吧。应该是三元表达式
ViewState["LastUpdate"]=2009-02-03?2009-02-03:DateTime.Now
[/Quote]


没写错,这是C#3.0的新特性:

return (DateTime)(ViewState["LastUpdate"] ?? DateTime.Now);

意思是:

如果 ViewState["LastUpdate"] 不等于 null,则返回 (DateTime)(ViewState["LastUpdate"]),
否则返回 DateTime.Now 。
zzxap 2009-03-23
  • 打赏
  • 举报
回复
原来这样

相当于SQL中的ISNULL(变量,默认值)
string a = b?? "-1";
b为null的时候,将"-1"赋值给a

我也没见过
zzxap 2009-03-23
  • 打赏
  • 举报
回复
写错了吧。应该是三元表达式
ViewState["LastUpdate"]=2009-02-03?2009-02-03:DateTime.Now

vrhero 2009-03-23
  • 打赏
  • 举报
回复
?? 运算符 - 如果??运算符的左操作数非null,该运算符将返回左操作数,否则返回右操作数。


[Quote=引用 3 楼 kkun_3yue3 的回复:]
只能在引用类型中使用,值类型永远不为null
如int,double,decimal,DateTime,float等
[/Quote]
泛型可空值类型也能用...

int? a;
return a??-1;
宝_爸 2009-03-23
  • 打赏
  • 举报
回复
The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

Remarks
A nullable type can contain a value, or it can be undefined. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable type to a non-nullable type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable type is currently undefined, an InvalidOperationException exception will be thrown.

For more information, see Nullable Types (C# Programming Guide).

Example
Copy Code
// nullable_type_operator.cs
using System;
class MainClass
{
static int? GetNullableInt()
{
return null;
}

static string GetStringValue()
{
return null;
}

static void Main()
{
// ?? operator example.
int? x = null;

// y = x, unless x is null, in which case y = -1.
int y = x ?? -1;

// Assign i to return value of method, unless
// return value is null, in which case assign
// default value of int to i.
int i = GetNullableInt() ?? default(int);

string s = GetStringValue();
// ?? also works with reference types.
// Display contents of s, unless s is null,
// in which case display "Unspecified".
Console.WriteLine(s ?? "Unspecified");
}
}


来自msdn
kkun_3yue3 2009-03-23
  • 打赏
  • 举报
回复
只能在引用类型中使用,值类型永远不为null
如int,double,decimal,DateTime,float等
fanbo 2009-03-23
  • 打赏
  • 举报
回复
帮你顶
kkun_3yue3 2009-03-23
  • 打赏
  • 举报
回复
相当于SQL中的ISNULL(变量,默认值)
string a = b?? "-1";
b为null的时候,将"-1"赋值给a
denglei12 2009-03-23
  • 打赏
  • 举报
回复
是条件运算符。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧