访问嵌套属性值的最佳解决方案是什么?
示例:
在某些情况下,它可能如下所示:
public void someFunction()
{
this.Device.ResponseHandler.Process(this.Device.TcpClient.responseMessage, this.Device.TcpClient.responseType)
}
我的解决方案是复制对象,只是为了缩短名称。
public void someFuntion()
{
// Just for shorten the access name afterwards
ResponseHandler responseHandler = this.Device.RepsonseHandler;
TcpClient tcpClient = this.Device.TcpClient;
responseHandler.Process(tcpClient.responseMessage, tcpClient.responseType);
}
它主要基于意见,但通常有两种方法:
您正在使用的那个。
在新行中列出每个参数:
public void someFunction()
{
this.Device.ResponseHandler.Process(
this.Device.TcpClient.responseMessage,
this.Device.TcpClient.responseType
);
}
IMO都具有相同的可读性,在第二种方法中,您不需要其他变量:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句