草庐IT

yield-keyword

全部标签

c# - using() { } block 中的 yield return 语句 在执行之前处理

我编写了自己的自定义数据层以持久保存到特定文件,并使用自定义DataContext模式对其进行了抽象。这一切都基于.NET2.0Framework(给定了目标服务器的限制),所以即使其中一些看起来像LINQ-to-SQL,但它不是!我刚刚实现了一个类似的数据模式。请参阅下面的示例,了解我还无法解释的情况。要获取Animal的所有实例-我这样做并且效果很好publicstaticIEnumerableGetAllAnimals(){AnimalDataContextdataContext=newAnimalDataContext();returndataContext.GetAllAni

c# - using() { } block 中的 yield return 语句 在执行之前处理

我编写了自己的自定义数据层以持久保存到特定文件,并使用自定义DataContext模式对其进行了抽象。这一切都基于.NET2.0Framework(给定了目标服务器的限制),所以即使其中一些看起来像LINQ-to-SQL,但它不是!我刚刚实现了一个类似的数据模式。请参阅下面的示例,了解我还无法解释的情况。要获取Animal的所有实例-我这样做并且效果很好publicstaticIEnumerableGetAllAnimals(){AnimalDataContextdataContext=newAnimalDataContext();returndataContext.GetAllAni

c# - 在 C# 中,为什么匿名方法不能包含 yield 语句?

我认为做这样的事情会很好(使用lambda进行yield返回):publicIListFind(Expression>expression)whereT:class,new(){IListlist=GetList();varfun=expression.Compile();varitems=()=>{foreach(variteminlist)if(fun.Invoke(item))yieldreturnitem;//ThisisnotallowedbyC#}returnitems.ToList();}但是,我发现我不能在匿名方法中使用yield。我想知道为什么。yielddocs就说

c# - 在 C# 中,为什么匿名方法不能包含 yield 语句?

我认为做这样的事情会很好(使用lambda进行yield返回):publicIListFind(Expression>expression)whereT:class,new(){IListlist=GetList();varfun=expression.Compile();varitems=()=>{foreach(variteminlist)if(fun.Invoke(item))yieldreturnitem;//ThisisnotallowedbyC#}returnitems.ToList();}但是,我发现我不能在匿名方法中使用yield。我想知道为什么。yielddocs就说

c# - "Use the new keyword if hiding was intended"警告

我的屏幕底部有一个警告:Warning1'WindowsFormsApplication2.EventControlDataSet.Events'hidesinheritedmember'System.ComponentModel.MarshalByValueComponent.Events'.Usethenewkeywordifhidingwasintended.C:\Users\myComputer\Desktop\EventControl\WindowsFormsApplication2\EventControlDataSet.Designer.cs11232eventContr

c# - "Use the new keyword if hiding was intended"警告

我的屏幕底部有一个警告:Warning1'WindowsFormsApplication2.EventControlDataSet.Events'hidesinheritedmember'System.ComponentModel.MarshalByValueComponent.Events'.Usethenewkeywordifhidingwasintended.C:\Users\myComputer\Desktop\EventControl\WindowsFormsApplication2\EventControlDataSet.Designer.cs11232eventContr

c# - 为什么 yield return 不能出现在带有 catch 的 try block 中?

以下是可以的:try{Console.WriteLine("Before");yieldreturn1;Console.WriteLine("After");}finally{Console.WriteLine("Done");}finallyblock在整个事情完成执行时运行(IEnumerator支持IDisposable以提供一种方法来确保这一点,即使枚举在完成之前被放弃)。但这不行:try{Console.WriteLine("Before");yieldreturn1;//errorCS1626:Cannotyieldavalueinthebodyofatryblockwit

c# - 为什么 yield return 不能出现在带有 catch 的 try block 中?

以下是可以的:try{Console.WriteLine("Before");yieldreturn1;Console.WriteLine("After");}finally{Console.WriteLine("Done");}finallyblock在整个事情完成执行时运行(IEnumerator支持IDisposable以提供一种方法来确保这一点,即使枚举在完成之前被放弃)。但这不行:try{Console.WriteLine("Before");yieldreturn1;//errorCS1626:Cannotyieldavalueinthebodyofatryblockwit

c# - 评估字符串 "3*(4+2)"yield int 18

这个问题在这里已经有了答案:Isitpossibletocompileandexecutenewcodeatruntimein.NET?(15个答案)关闭9年前。.NET框架是否有一个函数可以计算字符串中包含的数值表达式并返回结果?例如:stringmystring="3*(2+4)";intresult=EvaluateExpression(mystring);Console.Writeln(result);//Outputs18是否有一个标准的框架函数可以用来替换我的EvaluateExpression方法?

c# - 评估字符串 "3*(4+2)"yield int 18

这个问题在这里已经有了答案:Isitpossibletocompileandexecutenewcodeatruntimein.NET?(15个答案)关闭9年前。.NET框架是否有一个函数可以计算字符串中包含的数值表达式并返回结果?例如:stringmystring="3*(2+4)";intresult=EvaluateExpression(mystring);Console.Writeln(result);//Outputs18是否有一个标准的框架函数可以用来替换我的EvaluateExpression方法?