.net yield关键字 分类: .NET 201...

static void Main(string[] args)
        {
            //输出:2 4 8 16 32 64 128 256
            foreach (int i in Power(2, 8))
            {
                Console.Write("{0} ", i);
            }
        }
        public static IEnumerable<int> Power(int number, int exponent)
        {
            int result = 1;

            for (int i = 0; i < exponent; i++)
            {
                result = result * number;
                yield return result;
            }
        }


 /// <summary>
        /// 输出:
        /// Tadpole 400
        /// Pinwheel 25
        /// Milky Way 0
        /// Andromeda 3
        /// </summary>
        public static void ShowGalaxies()
        {
            var theGalaxies = new Galaxies();
            foreach (Galaxy theGalaxy in theGalaxies.NextGalaxy)
            {
                System.Diagnostics.Debug.WriteLine(theGalaxy.Name + " " + theGalaxy.MegaLightYears.ToString());
            }
        }

        public class Galaxies
        {
            public System.Collections.Generic.IEnumerable<Galaxy> NextGalaxy
            {
                get
                {
                    yield return new Galaxy { Name = "Tadpole", MegaLightYears = 400 };
                    yield return new Galaxy { Name = "Pinwheel", MegaLightYears = 25 };
                    yield return new Galaxy { Name = "Milky Way", MegaLightYears = 0 };
                    yield return new Galaxy { Name = "Andromeda", MegaLightYears = 3 };
                }
            }
        }

        public class Galaxy
        {
            public String Name { get; set; }
            public int MegaLightYears { get; set; }
        }

http://msdn.microsoft.com/zh-cn/library/9k7k7cf0.aspx


转载于:https://www.cnblogs.com/configman/p/4657546.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在pytest中,yield关键字用于实现fixture的teardown操作。fixture是在测试用例执行之前或之后执行的一段代码,用于进行一些预置操作或清理操作。通常情况下,teardown操作是通过在fixture函数中使用yield关键字来实现的。当测试用例调用fixture函数时,yield关键字会在测试用例执行完之后执行,用于执行teardown操作。在teardown操作中,可以进行一些清理工作,例如关闭浏览器、退出登录等。通过使用yield关键字,可以实现teardown操作的自动执行,并确保在每个测试用例执行之后进行必要的清理工作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [pytest系列——fixture之yield关键字实现teardown用例后置操作](https://blog.csdn.net/mashang_z111/article/details/126752230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [pytest使用yield关键字实现teardown_xxx的功能和request.addfinalizer()注册清理函数](https://blog.csdn.net/weixin_42550871/article/details/109137575)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值