Item 1: Use Properties Instead of Accessible Data Members(Effective C#)

  You can specify different accessibility modifiers to the get and set accessors in a property in C#.
  Indexers can be virtual or abstract; indexers can have separate access restrictions for setters and getters. You cannot create implicit indexers as you can with properties.

 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Linq;
 4  using  System.Text;
 5 
 6  namespace  EffectiveCSharpItem1
 7  {
 8       public   class  Person 
 9      {
10           private   string  name;
11           private  List < string >  friends  =   new  List < string > ();
12 
13           public   string  Name 
14          {
15               get  
16              {
17                   return  name;
18              }
19               protected   set  
20              {
21                   this .name  =  value;
22              }
23          }
24 
25           public   override   string  ToString()
26          {
27                return   this .name;
28          }
29 
30           public   string   this [ int  index] 
31          {
32               get  {  return  friends[index]; }
33               set  { friends[index]  =  value; }
34          }
35 
36           public  Person( string  name) 
37          {
38               this .Name  =  name;
39              friends.Add( " Summer " );
40          }
41      }
42 
43       class  Program
44      {
45           static   void  Main( string [] args)
46          {
47              Person p  =   new  Person( " Ray " );
48              Console.WriteLine(p.ToString());
49              Console.WriteLine(p[ 0 ]);
50 
51              Console.Read();
52          }
53      }
54  }

 

转载于:https://www.cnblogs.com/zhtf2014/archive/2011/01/08/1930767.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值