如何检查列表类是否为空 [英] How to check whether list class is empty or not

查看:89
本文介绍了如何检查列表类是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要检查列表类是否为空



我是使用以下内容





Hi,

I need to check whether list class is empty or not

I am using following for that


private CANBusDetails msgDetailsOfBus(CANBusDetails busDetails)
        {
            if (busDetails.Schedulability > 0)
                busDetails.Alpha = 0;

            busDetails.CANMessageDetailsList.Clear();
            foreach (CANMessageDetails Details in busDetails.OutputExcelMessageDetailsList)
            {
                CANMessageDetails tempMsgDetails = new CANMessageDetails();

                tempMsgDetails.MessageName = Details.MessageName;
                tempMsgDetails.HexMessageId = Details.HexMessageId;
                tempMsgDetails.TxNode = Details.TxNode;
                tempMsgDetails.CycleTime = Details.CycleTime;
                tempMsgDetails.CycleType = Details.CycleType;
                tempMsgDetails.Deadline = Details.Deadline;
                tempMsgDetails.Priority = Details.Priority;
                tempMsgDetails.Utilization = Details.Utilization;
                tempMsgDetails.BlockingTime = Details.BlockingTime;
                tempMsgDetails.BusyPeriod = Details.BusyPeriod;
                tempMsgDetails.WaitingTime = Details.WaitingTime;
                tempMsgDetails.NumberOfInvocations = Details.NumberOfInvocations;
                tempMsgDetails.WorstCaseInvocation = Details.WorstCaseInvocation;
                tempMsgDetails.WorstCaseResponseTime = Details.WorstCaseResponseTime;
                tempMsgDetails.ResponseTimeList.AddRange(Details.ResponseTimeList);
                tempMsgDetails.LatencyList.AddRange(Details.LatencyList);

                busDetails.CANMessageDetailsList.Add(tempMsgDetails);


            }
            return busDetails;

        }


           if (msgDetailsOfBus(SCANLA.Run.BusDetails[0]) == null)
                
                    NoValidBuses++;

                if (msgDetailsOfBus(SCANLA.Run.BusDetails[1]) == null)
               
                    NoValidBuses++;
                if (msgDetailsOfBus(SCANLA.Run.BusDetails[2]) == null)
               
                    NoValidBuses++;
                if (msgDetailsOfBus(SCANLA.Run.BusDetails[3]) == null)
                
                    NoValidBuses++

;



但是当列表class是空的,程序没有递增NoValidBuses

它显示了一些异常



如何解决这个问题?





谢谢

John

;

But when the list class is empty, the program is not incrementing NoValidBuses
It is showing some exception

How to solve this problem???


Thanks
John

推荐答案

来自看起来,该函数永远不会返回null,因为它只返回输入'bus details'类。如果输入为null,它将抛出异常。



如何做到以下其中一项:



From the looks of it, that function will never return null, as it simply returns the input 'bus details' class .. It will throw an exception if the input is null.

How about one of the following:

private bool msgDetailsOfBus(CANBusDetails busDetails)
{
    int count = 0;  // possible solution #2

    if (busDetails.Schedulability > 0)
        busDetails.Alpha = 0;
 
    busDetails.CANMessageDetailsList.Clear();

    // Possible solution #1... short-circuit return
    if (busDetails.OutputExcelMessageDetailsList.Count <= 0)
        return false;

    foreach (CANMessageDetails Details in busDetails.OutputExcelMessageDetailsList)
    {
       CANMessageDetails tempMsgDetails = new CANMessageDetails();
 
       tempMsgDetails.MessageName = Details.MessageName;
       tempMsgDetails.HexMessageId = Details.HexMessageId;
       tempMsgDetails.TxNode = Details.TxNode;
       tempMsgDetails.CycleTime = Details.CycleTime;
       tempMsgDetails.CycleType = Details.CycleType;
       tempMsgDetails.Deadline = Details.Deadline;
       tempMsgDetails.Priority = Details.Priority;
       tempMsgDetails.Utilization = Details.Utilization;
       tempMsgDetails.BlockingTime = Details.BlockingTime;
       tempMsgDetails.BusyPeriod = Details.BusyPeriod;
       tempMsgDetails.WaitingTime = Details.WaitingTime;
       tempMsgDetails.NumberOfInvocations = Details.NumberOfInvocations;
       tempMsgDetails.WorstCaseInvocation = Details.WorstCaseInvocation;
       tempMsgDetails.WorstCaseResponseTime = Details.WorstCaseResponseTime;
       tempMsgDetails.ResponseTimeList.AddRange(Details.ResponseTimeList);
       tempMsgDetails.LatencyList.AddRange(Details.LatencyList);
 
       busDetails.CANMessageDetailsList.Add(tempMsgDetails);

       count++;
    }
            
    return true;    // possible solution #1

    if (count > 0)
        return true;  // possible solution #2
}
 

if (msgDetailsOfBus(SCANLA.Run.BusDetails[0]) == false)
    NoValidBuses++;
 
if (msgDetailsOfBus(SCANLA.Run.BusDetails[1]) == false)
    NoValidBuses++;

if (msgDetailsOfBus(SCANLA.Run.BusDetails[2]) == false)               
    NoValidBuses++;

if (msgDetailsOfBus(SCANLA.Run.BusDetails[3]) == false)                
    NoValidBuses++


我假设CANMessageDetailsList是列表< T> 类型,所以

I'm assuming that CANMessageDetailsList is List<T> type, so
if(CANMessageDetailsList.Count > 0)
{
    // list is not empty..
}



正确我,如果我的假设是错的,否则它应该有效。



-KR


Correct me, if my assumption is wrong, otherwise it should work.

-KR


这篇关于如何检查列表类是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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