草庐IT

alias-method

全部标签

C# 语言设计 : method group inside `is` operator

我对C#语言的一些设计选择很感兴趣。C#规范中有一条规则允许使用方法组作为is的表达式。运算符(operator):classFoo{staticvoidMain(){if(MainisFoo)Main();}}如规范所述,上述条件始终为假:7.10.10Theisoperator•IfEisamethodgrouporthenullliteral,ofifthetypeofEisareferencetypeoranullabletypeandthevalueofEisnull,theresultisfalse.我的问题:允许在CLR中使用没有运行时表示的C#语言元素的目的/要点/原因

C# 编译器 : cannot access static method in a non-static context

我有下面的代码:publicclassAnything{publicintData{get;set;}}publicclassMyGenericBase{publicvoidInstanceMethod(Tdata){//dosomejob}publicstaticvoidStaticMethod(Tdata){//dosomejob}//othersmembers...}publicsealedclassUsefulController:MyGenericBase{publicvoidProxyToStaticMethod(){StaticMethod(null);}//others

c# - "Property set method not found"反射时出错

我正在尝试反射(reflect)一些类属性并以编程方式设置它们,但看起来我的PropertyInfo过滤器之一不起作用://Getallpublicorprivatenon-staticpropertiesdeclaredinthisclass(noinheritedproperties)-thathaveagetterandsetter.PropertyInfo[]props=this.GetType().GetProperties(BindingFlags.DeclaredOnly|BindingFlags.Instance|BindingFlags.Public|BindingF

c# - 错误 : "an object reference is required for the non-static field, method or property..."

这个问题在这里已经有了答案:CS0120:Anobjectreferenceisrequiredforthenonstaticfield,method,orproperty'foo'(9个回答)关闭5年前。我正在用C#创建一个应用程序。它的功能是评估给定的是否为素数以及相同的交换数是否也是素数。当我在VisualStudio中构建我的解决方案时,它说“非静态字段、方法或属性需要对象引用...”。我在使用“volteado”和“siprimo”方法时遇到了这个问题。问题出在哪里,我该如何解决?namespaceConsoleApplication1{classProgram{static

c# - 是什么导致这里出现 "extension methods cannot be dynamically dispatched"?

编译错误'System.Data.SqlClient.SqlConnection'hasnoapplicablemethodnamed'Query'butappearstohaveanextensionmethodbythatname.Extensionmethodscannotbedynamicallydispatched.Considercastingthedynamicargumentsorcallingtheextensionmethodwithouttheextensionmethodsyntax.现在,我知道如何解决该问题,但我正试图更好地了解错误本身。我有正在构建的类来利

c# - Equals Method 的默认行为是什么?

设A是一个类,其中一些成员为x、y、z:ClassA{intx;inty;Stringz;...}A是一个对象,因此它继承了对象中定义的“等于”函数。这个函数的默认行为是什么?它是检查成员的相等性还是检查引用的相等性? 最佳答案 ThedefaultimplementationofEqualssupportsreferenceequalityforreferencetypes,andbitwiseequalityforvaluetypes.Referenceequalitymeanstheobjectreferencesthatar

C# 错误 : "An object reference is required for the non-static field, method, or property"

我有两个类,一个用于定义算法参数,另一个用于实现算法:1类(算法参数):usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceVM_Placement{publicstaticclassAlgorithmParameters{publicstaticintpop_size=100;publicstaticdoublecrossover_rate=0.7;publicstaticdoublemutation_rate=0.001;publicstaticintchrom

c# - 错误 : "Cannot use ' async' on methods without bodies". 如何强制异步子覆盖?

我在一个系统上工作,在这个系统中,多个客户端对象需要通过一个接口(interface)实现一个特定的功能,我希望该功能与延续异步运行(我希望实现是I/O绑定(bind)的并希望确保所有客户端对象尽快完成此功能)。我正在使用VisualStudioAsyncCTPRefreshforSP1,使用C#“5.0”。在我的抽象类的子对象中强制执行异步行为的推荐做法是什么(见下文)?我不能(显然)使用虚拟方法方法强制使用“异步”方法。我只能要求“任务”返回类型。这是否意味着我根本不应该尝试在子对象中要求异步行为?在那种情况下,返回类型应该只是“void”吗?公共(public)接口(interf

时间:: console application - static methods

为什么在C#中,控制台应用程序,在默认的“程序”类中,所有方法都必须是静态的staticvoidMain(string[]args) 最佳答案 成员函数不必是静态的;但如果它们不是静态的,则需要您实例化一个Program对象才能调用成员方法。使用静态方法:publicclassProgram{publicstaticvoidMain(){System.Console.WriteLine(Program.Foo());}publicstaticstringFoo(){return"Foo";}}没有静态方法(换句话说,要求您实例化Pr

c# - += new EventHandler(Method) 与 += 方法

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:C#:Differencebetween‘+=anEvent’and‘+=newEventHandler(anEvent)’订阅事件有两种基本方式:SomeEvent+=newEventHandler(MyHandlerMethod);SomeEvent+=MyHandlerMethod;有什么区别,什么时候我应该选择一个而不是另一个?编辑:如果是一样的,那么为什么VS默认为长版本,使代码困惑?这对我来说毫无意义。