相关疑难解决方法(0)

正确使用'收益率'

产量关键字是其中的一个关键字,在C#是继续迷惑我,而且我正确使用它,我从来没有自信.

以下两段代码中,哪个是首选,为什么?

版本1:使用收益率返回

public static IEnumerable<Product> GetAllProducts()
{
    using (AdventureWorksEntities db = new AdventureWorksEntities())
    {
        var products = from product in db.Product
                       select product;

        foreach (Product product in products)
        {
            yield return product;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

版本2:返回列表

public static IEnumerable<Product> GetAllProducts()
{
    using (AdventureWorksEntities db = new AdventureWorksEntities())
    {
        var products = from product in db.Product
                       select product;

        return products.ToList<Product>();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# yield-return

873
推荐指数
13
解决办法
24万
查看次数

什么是"收益率突破"; 做C#?

我在MSDN中看到过这种语法:yield break但我不知道它是做什么的.有人知道吗?

.net c# yield

470
推荐指数
8
解决办法
13万
查看次数

'yield'关键字是一个语法糖吗?它的实施是什么?

可能重复:
yield语句实现

我见过msdn docs,它说:

yield关键字向编译器发出信号,表明它出现的方法是迭代器块.编译器生成一个类来实现迭代器块中表达的行为.在迭代器块中,yield关键字与return关键字一起使用,以向枚举器对象提供值.

所以它意味着yield关键字是一个语法糖,编译器完成了生成迭代器的繁重工作.(我对么 ?)

那么这个语法糖的生成实现代码是什么.

c# language-implementation yield-keyword

6
推荐指数
1
解决办法
854
查看次数

C#:如何翻译Yield关键字

  1. 没有yield关键字,MSDN示例会是什么样子?如果您愿意,可以使用任何示例.我只想了解幕后发生的事情.
  2. 产量运营商急切地懒洋洋地评估?

样品:

using System;
using System.Collections;
public class List
{
    public static IEnumerable Power(int number, int exponent)
    {
        int counter = 0;
        int result = 1;
        while (counter++ < exponent)
        {
            result = result * number;
            yield return result;
        }
    }

    static void Main()
    {
        // Display powers of 2 up to the exponent 8:
        foreach (int i in Power(2, 8))
        {
            Console.Write("{0} ", i);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# yield

4
推荐指数
2
解决办法
890
查看次数

有没有办法在C#/ LINQ中产生,发射或以其他方式实现以前空的数组?

我有一个动态数组字符串的对象,我已经实现如下:

public class MyThing { 
    public int NumberOfThings { get; set; }
    public string _BaseName { get; set; }
    public string[] DynamicStringArray {
        get {
            List<string> dsa = new List<string>();
            for (int i = 1; i <= this.NumberOfThings; i++) {
                dsa.Add(string.Format(this._BaseName, i));
            }
            return dsa.ToArray();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图在早些时候变得更冷,并实现了一些在LINQ中自动处理格式化数组列表的东西,但我设法失败了.

作为我尝试的事情的一个例子:

int i = 1;
// create a list with a capacity of NumberOfThings
return new List<string>(this.NumberOfThings)
    // create each of the things in the array dynamically
    .Select(x => string.Format(this._BaseName, i++)) …
Run Code Online (Sandbox Code Playgroud)

c# linq

4
推荐指数
1
解决办法
207
查看次数

为什么使用yield return的此代码进行编译?

如您所料,此代码会产生编译器错误:

public static IEnumerable<int> Foo()
{
}
Run Code Online (Sandbox Code Playgroud)

并非所有代码路径都返回值

但是,此编译仅带有有关无法访问代码的警告:

public static IEnumerable<int> Foo()
{
    if(false)
    {
        yield return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

这产生一个空的可枚举。为什么这样做有效,并且它是定义的行为?

c# ienumerable

4
推荐指数
1
解决办法
156
查看次数

关于Async和Await如何工作c#

我在这个网站上看到了一些关于Async和Await使用的帖子.很少有人说Async和Await在单独的后台线程上完成它的工作意味着产生一个新的后台线程,很少有人说没有意味着Async和Await没有启动任何单独的后台线程来完成它的工作.

所以任何人只要告诉我Async和Await在使用时会发生什么.

这是一个小程序

class Program
{
    static void Main(string[] args)
    {
        TestAsyncAwaitMethods();
        Console.WriteLine("Press any key to exit...");
        Console.ReadLine();
    }

    public async static void TestAsyncAwaitMethods()
    {
        await LongRunningMethod();
    }

    public static async Task<int> LongRunningMethod()
    {
        Console.WriteLine("Starting Long Running method...");
        await Task.Delay(5000);
        Console.WriteLine("End Long Running method...");
        return 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是:

Starting Long Running method...
Press any key to exit...
End Long Running method...
Run Code Online (Sandbox Code Playgroud)

.net c# task-parallel-library async-await

3
推荐指数
2
解决办法
388
查看次数

收益表对计划流程的影响

我正在尝试理解在C#中使用yield关键字,因为我正在使用的队列建模包广泛使用它.

为了演示yield的用法,我正在使用以下代码:

using System;
using System.Collections.Generic;
public class YieldTest
{
    static void Main()
    {
        foreach (int value in ComputePower(2, 5))
        {
            Console.Write(value);
            Console.Write(" ");
        }
        Console.WriteLine();
    }
    /**
     * Returns an IEnumerable iterator of ints
     * suitable for use in a foreach statement
     */
    public static IEnumerable<int> ComputePower(int number, int exponent)
    {
        Console.Write ("Arguments to ComputePower are number: " + number + " exponent: " + exponent + "\n");
        int exponentNum = 0;
        int numberResult = 1;
        while (exponentNum < …
Run Code Online (Sandbox Code Playgroud)

c# yield-return

2
推荐指数
1
解决办法
526
查看次数