处理一个空的最佳方法 [英] Best way to handle a NULL

查看:121
本文介绍了处理一个空的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的函数前,我试图处理进入我在C#程序一个空的最佳方式。这是检查和处理空,为什么最好的方式?我已经添加了我现在使用什么,ReSharper的告诉我使用选项#1的完整代码。通常我做什么它说,我明白为什么它使得它更高效。这一次,虽然我不知道,所以我必须要问。

 选项#1 
如果(发件人== NULL )返回;

//代码放在这里



选项#2
如果(发送!= NULL)
$ { b $ b //代码放在这里
}

完整代码
私人无效EmployeeMouseHoverToolTip(对象发件人,EventArgs五)
{
如果(发送者! = NULL)
{
VAR sUserIdentifier =((C1TextBox)发送方)。文本;
VAR UserIdentifier的= Guid.Empty;
如果(Utilities.IsGuid(sUserIdentifier))
{
UserIdentifier的=新的GUID(sUserIdentifier);
}

变种的ToolTipText = Utilities.UserIdentifierToName(UserIdentifier的);
c1SuperTooltip.SetToolTip(发件人为C1TextBox,的ToolTipText);
}
}


解决方案

这是事件处理程序,它应该只被控制在响应事件称为(不能直接通过你自己的代码),所以你不应该在乎null检查,甚至键入的发件人检查参数(如果只重视此事件处理函数相同类型的控件)。我这样做只是这样的:

 私人无效EmployeeMouseHoverToolTip(对象发件人,EventArgs五){
变种txtBox =(C1TextBox)发送;
VAR sUserIdentifier = txtBox.Text;
VAR UserIdentifier的= Utilities.IsGuid(sUserIdentifier)?
新的GUID(sUserIdentifier):
Guid.Empty;
变种的ToolTipText = Utilities.UserIdentifierToName(UserIdentifier的);
c1SuperTooltip.SetToolTip(txtBox,的ToolTipText);
}



其实,我走一步,并分离逻辑来获得从逻辑提示文本阅读和更新UI。事情是这样的:

 私人无效EmployeeMouseHoverToolTip(对象发件人,EventArgs五){
VAR txtBox =(C1TextBox)发件人;
变种的ToolTipText = ResolveUpdatedTooltipText(txtBox.Text);
c1SuperTooltip.SetToolTip(txtBox,的ToolTipText);
}

私人字符串ResolveUpdatedTooltipText(字符串sUserIdentifier){
VAR UserIdentifier的= ResolveGuid(sUserIdentifier);
返回Utilities.UserIdentifierToName(UserIdentifier的);
}

私人的Guid ResolveGuid(字符串sUserIdentifier){
返回Utilities.IsGuid(sUserIdentifier)?
新的GUID(sUserIdentifier):
Guid.Empty;
}



所以,你不应该使用任何您所提供的选项。


At the top of my functions I'm trying the best way to handle a null coming into my procedures in C#. Which is the best way for checking and handling the null and why? I've added the complete code of what I'm using right now and Resharper is telling me to use Option #1. Normally I do what it says as I understand why it makes it more efficient. This time though I'm not sure so I must ask.

Option #1
if (sender == null) return;

// Code goes here

or

Option #2
if (sender != null)
{ 
     // Code goes here
}

Complete Code
        private void EmployeeMouseHoverToolTip(object sender, EventArgs e)
        {
            if (sender != null)
            {
                var sUserIdentifier = ((C1TextBox)sender).Text;
                var userIdentifier = Guid.Empty;
                if (Utilities.IsGuid(sUserIdentifier))
                {
                    userIdentifier = new Guid(sUserIdentifier);
                }

                var toolTipText = Utilities.UserIdentifierToName(userIdentifier);
                c1SuperTooltip.SetToolTip(sender as C1TextBox, toolTipText);
            }
        }

解决方案

This is an event handler, it should only be called by controls in response to an event (never directly by your own code), so you shouldn't care about null checks or even type checks on the sender parameter (if you only attach this event handler to the same type of control). I'd do it simply like this:

private void EmployeeMouseHoverToolTip(object sender, EventArgs e) {  
  var txtBox = (C1TextBox)sender;
  var sUserIdentifier = txtBox.Text;
  var userIdentifier = Utilities.IsGuid(sUserIdentifier) ? 
    new Guid(sUserIdentifier) : 
    Guid.Empty;
  var toolTipText = Utilities.UserIdentifierToName(userIdentifier);
  c1SuperTooltip.SetToolTip(txtBox, toolTipText);
}

Actually, I'd go one step further and separate the logic to get the tooltip text from the logic to read and update the UI. Something like this:

private void EmployeeMouseHoverToolTip(object sender, EventArgs e) {  
  var txtBox = (C1TextBox)sender;
  var toolTipText = ResolveUpdatedTooltipText(txtBox.Text);
  c1SuperTooltip.SetToolTip(txtBox, toolTipText);
}

private string ResolveUpdatedTooltipText(string sUserIdentifier) {
  var userIdentifier = ResolveGuid(sUserIdentifier);
  return Utilities.UserIdentifierToName(userIdentifier);
}

private Guid ResolveGuid(string sUserIdentifier) {
  return Utilities.IsGuid(sUserIdentifier) ? 
    new Guid(sUserIdentifier) : 
    Guid.Empty;
}

Therefore, you shouldn't use any of the options you provided.

这篇关于处理一个空的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆