Why compile error “Use of unassigned local variable”?

前端 未结 10 2204
说谎
说谎 2020-11-22 03:16

My code is the following

int tmpCnt;  
if (name == \"Dude\")  
   tmpCnt++;  

Why is there an error Use of unassigned local variabl

10条回答
  •  死守一世寂寞
    2020-11-22 04:10

    IEnumerable _getCurrentHolidayList; //this will not initailize
    

    Assign value(_getCurrentHolidayList) inside the loop

    foreach (HolidaySummaryList _holidayItem in _holidayDetailsList)
    {
                                if (_holidayItem.CountryId == Countryid)
                                    _getCurrentHolidayList = _holidayItem.Holiday;                                                   
    }
    

    After your are passing the local varibale to another method like below. It throw error(use of unassigned variable). eventhough nullable mentioned in time of decalration.

    var cancelRescheduleCondition = GetHolidayDays(_item.ServiceDateFrom, _getCurrentHolidayList);
    

    if you mentioned like below, It will not throw any error.

    IEnumerable _getCurrentHolidayList =null;
    

提交回复
热议问题