C#中问号的用法(非表达式)---单问号,双问号

单问号---用于给变量设初值的时候,给变量(int类型)赋值为null,而不是0!

双问号---用于判断并赋值,先判断当前变量是否为null,如果是就可以赋一个新值,否则跳过!

 1 /*
 2  * Created by BpLoveGcy.cnblogs.com
 3  * Gump Yin
 4  * Date: 2010-3-28
 5  * Time: 13:03
 6  * 
 7  * Version:
 8  * CopyRight:http://BpLoveGcy.cnblogs.com/
 9  */
10 
11 using System;
12 using NUnit.Framework;
13 
14 namespace MustKnownDotNet
15 {
16     public class QuestionMark
17     {
18         private int? id;
19 
20         public int? NULL_ID
21         {
22             get
23             {
24                 return this.id;
25             }
26         }
27         public int? NotNULL_ID
28         {
29             get
30             {
31                 return this.id ?? 1;
32             }
33         }
34     }
35     [TestFixture]
36     public class QuestionMark_Test
37     {
38         [Test]
39         public void TestMethod()
40         {
41             // TODO: Add your test.
42             QuestionMark q = new QuestionMark();
43             Assert.IsNotNull(q.NotNULL_ID);
44             Assert.IsTrue(q.NotNULL_ID == 1);
45 
46         }
47         [Test]
48         public void TestMethod1()
49         {
50             // TODO: Add your test.
51             QuestionMark q = new QuestionMark();
52 
53             Assert.IsNull(q.NULL_ID);
54         }
55     }
56 }
57 

 

ref:http://space.itpub.net/12639172/viewspace-503457 

posted @ 2010-03-28 13:09  Freedom  阅读(1077)  评论(0编辑  收藏  举报