小编Emi*_*sen的帖子

C#使用IF/ELSE语句中Method的返回值

现在我正在制作一个简单的程序,这是我多次思考过的问题.很多次我运行我的方法两次是因为在运行它们之前检查返回值,我想知道是否有一种方法可以防止这种情况,比如使用我正在检查的方法返回的值.这很难解释,所以这是我的程序的真实例子.

public class SFDBRepository
{
    public static Domain.SF.SFObject GetSFOrder(string WorkOrd)
    {
        //As you can see here i'm checking on the output of this method, before trying to return it.
        if (Domain.SF.SF.GetOrder(WorkOrd) != null)
        {
            //If the value is not null (My method returns null if no result), return the object
            return Domain.SF.SF.GetOrder(WorkOrd);
        }
        //Same thing happens here. My method runs twice every time almost. 
        else if(Domain.Building_DeliveryPerformance.Building_DeliveryPerformance.GetObject(WorkOrd) != null)
        {
            return Domain.Building_DeliveryPerformance.Building_DeliveryPerformance.GetObject(WorkOrd);
        }
        else
        {
            return null;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

.net c# syntax if-statement

0
推荐指数
1
解决办法
788
查看次数

标签 统计

.net ×1

c# ×1

if-statement ×1

syntax ×1