返回空引用(null)还是返回零元素实例?

对于一个返回值是集合类型的方法,当处理结果是空值的时候,返回null还是零元素的实例?

1。返回零元素的实例《源码来源于Petshop4》

ExpandedBlockStart.gif ContractedBlock.gif          public  IList < ItemInfo >  GetItemsByProduct( string  productId)  dot.gif {
InBlock.gif
InBlock.gif            IList
<ItemInfo> itemsByProduct = new List<ItemInfo>();  // “立即加载”
InBlock.gif

InBlock.gif            SqlParameter parm 
= new SqlParameter(PARM_PRODUCT_ID, SqlDbType.VarChar, 10);
InBlock.gif            parm.Value 
= productId;
InBlock.gif
InBlock.gif            
//Execute the query against the database
ExpandedSubBlockStart.gifContractedSubBlock.gif
            using(SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEMS_BY_PRODUCT, parm)) dot.gif{
InBlock.gif                
// Scroll through the results
ExpandedSubBlockStart.gifContractedSubBlock.gif
                while (rdr.Read()) dot.gif{
InBlock.gif                    ItemInfo item 
= new ItemInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetInt32(2), rdr.GetDecimal(3), rdr.GetString(4), rdr.GetString(5), rdr.GetString(6), rdr.GetString(7));
InBlock.gif                    
//Add each item to the arraylist
InBlock.gif
                    itemsByProduct.Add(item);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return itemsByProduct;
ExpandedBlockEnd.gif        }


2。返回空值《记得以前有人称其为“延迟加载”》

ExpandedBlockStart.gif ContractedBlock.gif          public  IList < ItemInfo >  GetItemsByProduct( string  productId)  dot.gif {
InBlock.gif
InBlock.gif            IList
<ItemInfo> itemsByProduct = null;  // “延迟加载”
InBlock.gif

InBlock.gif            SqlParameter parm 
= new SqlParameter(PARM_PRODUCT_ID, SqlDbType.VarChar, 10);
InBlock.gif            parm.Value 
= productId;
InBlock.gif
InBlock.gif            
//Execute the query against the database
ExpandedSubBlockStart.gifContractedSubBlock.gif
            using(SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_SELECT_ITEMS_BY_PRODUCT, parm)) dot.gif{
InBlock.gif                
if (rdr.HasRow)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    itemsByProduct 
= new IList<ItemInfo>(); // “延迟加载”
ExpandedSubBlockEnd.gif
                }

InBlock.gif
InBlock.gif                
// Scroll through the results
ExpandedSubBlockStart.gifContractedSubBlock.gif
                while (rdr.Read()) dot.gif{
InBlock.gif                    ItemInfo item 
= new ItemInfo(rdr.GetString(0), rdr.GetString(1), rdr.GetInt32(2), rdr.GetDecimal(3), rdr.GetString(4), rdr.GetString(5), rdr.GetString(6), rdr.GetString(7));
InBlock.gif                    
//Add each item to the arraylist
InBlock.gif
                    itemsByProduct.Add(item);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return itemsByProduct;
ExpandedBlockEnd.gif        }

直观上,返回零元素的集合实例,需要无味的开销,但是很多时候返回零元素比返回空值更方便,也更具健壮性,因为很多情况下当参数为null,会抛出“参数无效”异常。
比如,当你将集合绑定到DataGrid的时候,只有当集合是非空引用的时候,才会创建DataGridItem项,当DataGrid的DataSource是null的时候,是不会创建任何项的,连Header和Footer都不会创建,此时界面上就是空白信息,因此,我们就需要手动处理,告诉用户:无任何数据。

但是,偶目前能然没有非常好的测试实例,或者“best pratices”的建议,欢迎大家抛砖^_^。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值