首页 > 其他 > 详细

总结下遇到的C#新语法

时间:2014-02-23 05:34:28      阅读:284      评论:0      收藏:0      [点我收藏+]

1.属性的private set读写器

bubuko.com,布布扣
public int x { get; private set; }
bubuko.com,布布扣

是另一种简写, set前面的private声明是告诉编译器属性X是只读(read-only)的. 意思是对于外部类不能通过属性X给x赋值,而只能读取其值。

例子:

bubuko.com,布布扣
 1 public class PswChangingEventArgs : EventArgs
 2     {
 3         public readonly string message;
 4 
 5         public PswChangingEventArgs(string _message)
 6         {
 7             this.message = _message;
 8         }
 9     }
10 
11     public class TestClass
12     {
13         public event EventHandler<PswChangingEventArgs> PasswordChanging;   // 普通非泛型EventHandler不支持自定义EventArgs
14 
15         private string _pwd;
16         public string Pwd
17         {
18             get { return _pwd; }
19             private set
20             {
21                 if (string.IsNullOrEmpty(value))
22                 {
23                     throw new ArgumentException("password cannot be empty");
24                 }
25                 if (_pwd != value)
26                 {
27                     if (PasswordChanging != null) PasswordChanging(this, new PswChangingEventArgs("password已经改变"));
28                 }
29                 _pwd = value;
30             }
31         }
32 
33         public void Fun()
34         {
35             this._pwd = "abc";                 // 直接赋值,什么额外事情都不会发生
36             this.Pwd = "efg";                  // 可以引发密码更改事件,可以进行校验
37         }
38     }
39 
40  class Program
41     {
42         static void Main(string[] args)
43         {
44             TestClass a = new TestClass();
45             a.PasswordChanging += new EventHandler<PswChangingEventArgs>(ShowMessage);
46             a.Fun();
47             //a.Pwd = "1111";           //error,private set:外部不可写入
48             Console.WriteLine("执行Fun()后password:" + a.Pwd);   //ok,外部可以读取
49 
50             Console.Read();
51         }
52 
53         static void ShowMessage(object sender, PswChangingEventArgs e)
54         {
55             Console.WriteLine("执行Main()里的ShowMessage方法");
56             Console.WriteLine("sender:"+sender.ToString() + ",message:" + e.message);
57         }
58     }
bubuko.com,布布扣

①.若直接用set,则类外部也可以访问通过Pwd属性读取、写入pwd字段了,扩大了访问权限,破坏了封装、隐蔽性

②.若不用set,则外部只可访问不可写入,但类内部方法虽可读取写入,但写入时不会应发对应操作,不便

③.为了对外隐藏pwd字段的写入(仍可读取),对内可以在写入时设定一些操作。

2.i??操作符:

?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types.‘ data-guid="60cc2d770bb1adb4814234741973b2db"> ?? 运算符称为 null 合并运算符,用于定义可以为 null 值的类型和引用类型的默认值。 如果此运算符的左操作数不为 null,则此运算符将返回左操作数;否则返回右操作数。

例子:

bubuko.com,布布扣
1             //int i = null; //error
2             int? i = null;
3             Console.WriteLine(i);
4             int j = i ?? 1;
5             //等价如下:
6             //int j = i.HasValue ? i.Value : 1;
7             Console.WriteLine(i);
8             Console.WriteLine(j);
bubuko.com,布布扣

3.默认参数,例如:

  Fun(int a=1),调用时会看到提示Fun([int a=1]),a参数指不指定都合法。

bubuko.com,布布扣
 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             TestMethod("Foo");
 6             TestMethod("Foo", "Bar1");
 7             TestMethod("Foo", "Bar1", "Baz1");
 8 
 9             Console.Read();
10         }
11 
12         static void TestMethod(string foo, string bar = "Bar", string baz = "Baz")
13         {
14             Console.WriteLine("{0, -5} - {1, -5} - {2, -5}", foo, bar, baz);
15         }
16     }
bubuko.com,布布扣

总结下遇到的C#新语法

原文:http://www.cnblogs.com/nlh774/p/3561103.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!