C# list count always returns 1 even when list is empty

萝らか妹 提交于 2021-02-04 17:49:07

问题


I'm trying to debug a method in C# but my basic syntax skills here seem to be lacking! The method accepts a list of dates as a comma-separated text string. This string is converted to a list, then processed. However, it seems that even when an empty string is passed to the method, it still outputs 1 when the list is counted.

The code is as follows:

public static int DaysLeft(DateTime endDate, DateTime startDate, Boolean excludeWeekends, String excludeDates)
    {
        int counter = 0;

        List<string> excludeDatesList = new List<string>(excludeDates.Split(','));

        counter = excludeDatesList.Count;

        return counter;
    }

If I pass an empty string in as the excludeDates parameter, it returns 1. If I pass a single date it returns 1. If I pass two dates, it returns 2 etc. So it's kind of working except where there's nothing passed in, when I'd expect it to return 0 but it actually returns 1.

Can anyone point me in the right direction?

Thanks


回答1:


Even for an empty string, Split will return that string in the array, so the list will be created with... one empty string, producing a .Count of 1. [Edit: You can call excludeDates.Split(',', StringSplitOption.RemoveEmptyEntries) so that it doesn't.]

To make your function behave as you expect, you should probably try to parse each "date" string returned from Split(), and only increment the counter for valid dates.

Something like this:

    int counter = 0;
    var possibleDates = excludeDates.Split(',');

    foreach (var dateStr in possibleDates)
    {
        // Right now it just counts "good" dates, though could also do something
        //  with each date as well
        DateTime dt;
        if (DateTime.TryParse(dateStr, out dt))
            counter++;
    }

    return counter;

If you're looking for the simplest way, you should just check the parameter to see if it's the empty string, and return 0 in that case:

if (string.IsNullOrEmpty(excludeDates))
    return 0;



回答2:


Splitting on the character is returning an empty element.

Try using excludeDates.Split(',', StringSplitOptions.RemoveEmptyEntries) instead.




回答3:


You can use the remove empty entries option.

var blah = "";

var split = blah.Split(new[]{';'}, StringSplitOptions.RemoveEmptyEntries);
var split2 = blah.Split(new[]{';'});

// Returns zero
Console.WriteLine(split.Length);

// Returns one
Console.WriteLine(split2.Length);



回答4:


That is normal behavior. When there is nothing to split it will return a list with the string itself as the first element which counts as one.




回答5:


The array returned by String.Split() always has one element in it, even if it is the empty string.




回答6:


Inserting an empty string into the list will create a list of one element. That element would be your empty string.




回答7:


The result of calling string.Split on a string that doesn't contain the delimiter (in this case, a comma), is an array containing a single element, namely the original string. This also happens if the string is empty.

The solution is to specify Split to omit empty entries:

List<string> excluseDatesList = new List<string>(excludeDates.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));



回答8:


As dlev said.. but to add an example to illustrate, if you pass in the string "," - which is just a comma, then ",".Split(',').Count() will return 2. for ",,,," which is just four commas, you will get back 5 for the count...




回答9:


if you look at the Split method, it has the following logic:

int num = this.MakeSeparatorList(separator, ref sepList);
if (num == 0 || count == 1)
{
    return new string[]
    {
        this
    };
}

Therefore, even if the string is an empty string it will return a single item in the array.



来源:https://stackoverflow.com/questions/6763024/c-sharp-list-count-always-returns-1-even-when-list-is-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!