该产量关键字是其中的一个关键字,在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) 样品:
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)
我有一个动态数组字符串的对象,我已经实现如下:
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) 如您所料,此代码会产生编译器错误:
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)
这产生一个空的可枚举。为什么这样做有效,并且它是定义的行为?
我在这个网站上看到了一些关于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) 我正在尝试理解在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)