Effective C# Rule number 1

今天开始学C#, 老板让我两周之内搞定。不过还好我有Java 和c++基础大笑。我学Java 的时候就看这本书,没想到C#也有。 这样我每天学一条,分享给大家。
1. Always Use Properties Instead of Accessible Data Members

在C#里,Property已经晋升为一类公民。如果你的类里还有Public的变量,Stop! 如果你还在手写get and set 方法,Stop! Property在不破坏你类的封装的情况下,仍可以把类的datamember变成public interface的一部分。访问Property的方式和访问data member的方式一样,但Property是用methods实现的。


Properties let you expose data members as part of your public interface and still provide the encapsulation you want in an object-oriented environment. Client code accesses properties as though they are accessing public variables.

The .NET Framework assumes that you'll use properties for your public data members. In fact, the data binding code classes in the .NET Framework, especially WPF, support properties, not public data members. Data binding ties a property of an object to a user interface control. But that doesn't mean properties should be used exclusively in UI logic. You should still be using properties for other classes and structures. Properties are far easier to change as you discover new requirements or behaviors over time. See classPerson below.

With properties, adding multithreaded support is easy. Simply enhance the implementation of the get and set methods to provide synchronized access to the data. See classPersonThreadSafe below.

Properties have all the language features of methods. Properties can be virtual, abstract, and also part of an interface definition.


namespace Item1NS

{

   // Properties are far easier to change as you add or improve behavior

   classPerson

    {

       privatestring _FirstName;

       publicstring FirstName

        {

           get {return _FirstName; }

           set

            {

               if (string.IsNullOrEmpty(value))

                   thrownewArgumentException("FirstName cannot be null or empty");

               else

                    _FirstName =value;

            }

        }

    }

 

   // Adding multi-threading support to properties is also easy

   classPersonThreadSafe

    {

       privatestring _FirstName;

       privateobject _lock = newobject();

       publicstring FirstName

        {

           get {return _FirstName; }

           set

            {

               if (string.IsNullOrEmpty(value))

                   thrownewArgumentException("FirstName cannot be null or empty");

               else

                   lock (_lock)

                    {

                        _FirstName =value;

                    }

            }

        }

    } 

}

总而言之,当你想让你类内部的数据被外界访问到时(不管是public还是protected),一定要用Property。对于序列和字典,使用indexer。你类的datamember永远应该是private,绝无例外。使用Property,你可以得到如下好处:

1.Databinding支持

2.对于需求变化有更强的适应性,更方便的修改实现方法

记住,现在多花1分钟使用Property,会在你修改程序以适应设计变化时,为你节约n小时。得意



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值