如何验证IEnumerable中的收益率默认收益? [英] How to validate yield return default in IEnumerable?

查看:96
本文介绍了如何验证IEnumerable中的收益率默认收益?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查 IEnumerable<> 是否为空或为空,但是无论如何我的if检查总是失败,只要它为空.

I am trying to check whether IEnumerable<> is null or empty but somehow my if check always fails whenever it is empty.

    private bool Update()
    {
        IEnumerable<RecordHolder> recordHolders = GetData();
        // below  check doesn't work
        if (recordHolders == null || !recordHolders.Any())
            return false;

        // .. some other code
    }

    public IEnumerable<RecordHolder> GetData()
    {
        var isSuccess = PullRemote(url);
        if (!isSuccess.Result) { yield return default; }

        // .. some other code
    }

每当我的 GetData()方法返回 yield返回默认值时,随后的 if 签入 Update 方法都不会不行我在做什么错了?

Whenever my GetData() method returns yield return default then the subsequent if check in Update method doesn't work. Anything wrong I am doing?

我以为如果在 Update 方法中签入,将能够捕获 yield返回默认值,但是我看起来是错的.

I thought my if check in Update method will be able to catch yield return default but I am wrong looks like.

推荐答案

我认为您正在寻找屈服点,而不是 yield return default .或者,您可以使用 regular 方法(不是迭代器块)执行操作的第一部分,并在特殊情况下返回失败情况( Array.Empty< T>()是常见的选择),对于成功的案例,应使用迭代器块(或 Select 投影).

I think you are looking for yield break instead of yield return default. Alternatively, you can do the first part of the operation in a regular method (not an iterator block), and special-case the return for failure cases (Array.Empty<T>() is a common choice), deferring to an iterator block (or a Select projection) for the success cases.

这篇关于如何验证IEnumerable中的收益率默认收益?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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