草庐IT

anonymous-methods

全部标签

c# - "public new virtual void Method()"是什么意思?

什么时候使用新的虚关键字修饰方法?什么是感情?比如定义一个接口(interface),然后添加一个类来继承这个接口(interface)。而是使用新的virtual来实现接口(interface)方法。interfaceIPrinter{voidPrint();}publicclassPrinterOne:IPrinter{publicvoidPrint(){Console.WriteLine("PrinterOne.");}}publicclassPrinterTwo:PrinterOne{publicnewvirtualvoidPrint(){Console.WriteLine("

c# - 分页列表错误 : The method 'OrderBy' must be called before the method 'Skip'

完整的错误信息如下:“Skip”方法仅支持LINQtoEntities中的排序输入。方法'OrderBy'必须在方法'Skip'之前调用在“PurchaseOrderController”中,我已将这段代码添加到索引方法中://GET:PurchaseOrderpublicActionResultIndex(int?page){returnView(db.PurchaseOrders.ToPagedList(page??1,3));}还在“PurchaseOrders”的索引View中,我添加了这段代码:@usingPagedList;@usingPagedList.Mvc;@mode

c# - 为什么在匿名方法中不允许使用 out 参数?

这不是Callingamethodwithreforoutparametersfromananonymousmethod的骗局我想知道为什么out参数在匿名方法中是不允许的。不允许ref参数对我来说更有意义,但是out参数,就没那么多了。你对此有何看法 最佳答案 在某些方面,这是一个骗局。Out参数是ref参数。C#语言使用的值只是一个额外的属性。不允许它们的原因与ref参数完全相同。这里的问题源于在匿名方法中使用在匿名方法之外声明的值的效果。这样做会捕获lambda中的值,并且出于必要而任意延长其生命周期,使其超出当前函数的生命周

c# - Lambda\Anonymous 函数作为参数

我是C#的新手。只是玩弄它。并非出于真正目的。voidmakeOutput(int_param){Console.WriteLine(_param.ToString());}//...//Somewhereinacode{makeOutput(/*somenotc#codeforanexampleforwhatdoIwant*/function:int(){return0;});}是否可以使用真正的匿名函数(意味着返回结果)?我不想使用这样的委托(delegate)//Somewhereinacode{Funcx=()=>{return0;};makeOutput(x())}我也不想更

c# - 委托(delegate) : Method name expected error

我正在尝试让以下简单的委托(delegate)示例正常工作。根据我从中获取的一本书应该没问题,但我得到了一个Methodnameexpected错误。namespaceTestConsoleApp{classProgram{privatedelegatestringD();staticvoidMain(string[]args){intx=1;Dcode=newD(x.ToString());}}}有什么帮助吗? 最佳答案 删除():Dcode=newD(x.ToString);您想指定方法,而不是执行。

c# - .NET 4.5 中的代码契约 + 异步 : "The method or operation is not implemented"

在Windows7x64上的VS2012中使用CodeContracts1.4.51019.0时,我从ccrewrite收到以下编译错误:“方法或操作未实现."这似乎是由属性访问器的组合和使用缺少内部await的async方法引起的。复制步骤:创建一个启用“完整”运行时契约检查的新类库:namespaceCodeContractsAsyncBug{usingSystem.Threading.Tasks;publicclassService{//Offendingmethod!publicasyncTaskProcessAsync(Entityentity){varflag=entity

c# - LINQ vs Lambda vs 匿名方法 vs 委托(delegate)

谁能解释一下LINQ、Lambda、匿名方法和委托(delegate)的含义?这三者有何不同?一个可以替换另一个吗?我在谷歌搜索时没有得到任何具体答案 最佳答案 LINQ是一个宽泛的技术名称,涵盖了.NET3.5的大部分内容和C#3.0的变化;“用语言查询”等等。委托(delegate)相当于函数指针;一个“方法句柄”作为一个对象,如果你愿意的话,即Funcadd=(a,b)=>a+b;是一种编写我随后可以调用的委托(delegate)的方法。委托(delegate)还支持事件和其他回调方法。匿名方法是用于创建委托(delegate

c# - 您如何在 MVC3 中的不同 Action Methods 调用之间保持全局变量的值?

我正在使用Razor和C#开发ASP.NETMVC3Web应用程序。我刚刚发现我对全局变量有一些问题,可能是因为我对MVC比较陌生。我有一个带有一些全局变量和操作方法的Controller。我声明了一个全局变量,以便允许操作方法对其进行操作并将操作反射(reflect)到所有操作方法。我有以下情况:publicclassmyController:Controller{privatestring_MyGlobalVariable;publicActionResultIndex(){_MyGlobalVariable="Hello";//othercodereturnView("MyVie

c# - 为什么 Linq to Entity Select Method 翻转投影列表属性?

我对linqtoentity/Json/MVC.net4有最奇怪的行为我有这段代码,出于某种奇怪的原因,所有其他列表的属性顺序都颠倒了。varoutput=db.FooBar.Where(a=>a.lookupFoo==bar).Select(a=>newList{//value'sarethesameperrow//fordemonstrationsake.a.fooBarA,//Always12.34a.fooBarB,//Always12.34a.fooBarC,//Always0a.fooBarD//Always0//lazycastingtodoublefromint});r

c# - 为什么匿名委托(delegate)/lambda 不推断 out/ref 参数的类型?

StackOverflow上的几个C#问题询问如何使用out或ref参数制作匿名委托(delegate)/lambda。参见,例如:CallingamethodwithreforoutparametersfromananonymousmethodWritealambdaoranonymousfunctionthatacceptsanoutparameter为此,您只需指定参数的类型,如:publicvoiddelegateD(outTp);//...Da=(outTt)=>{...};//Lambdasyntax.Db=delegate(outTt){...};//Anonymousd