草庐IT

X-Method-Override

全部标签

C# 设计 : Why is new/override required on abstract methods but not on virtual methods?

为什么抽象方法需要new/override而虚方法不​​需要?示例1:abstractclassShapesClass{abstractpublicintArea();//abstract!}classSquare:ShapesClass{intx,y;publicintArea()//Error:missing'override'or'new'{returnx*y;}}编译器会显示这个错误:要使当前成员覆盖该实现,请添加override关键字。否则添加新关键字示例2:classShapesClass{virtualpublicintArea(){return0;}//itisvirt

c# - 低级差异 : non-static class with static method vs. 静态类与静态方法

我想知道使用具有静态方法的非静态类与具有相同静态方法的静态类的一般好处(或缺点)是什么,除了我不能使用非静态类中的静态方法作为扩展方法。例如:classNonStaticClass{publicstaticstringGetData(){return"Thiswasinvokedfromanon-staticclass.";}}与此相比:staticclassStaticClass{publicstaticstringGetData(){return"Thiswasinvokedfromastaticclass.";}}使用一种方法优于另一种方法对性能/内存有何影响?注意:假设我不需要

c# - Resharper 中的警告 "Return value of pure method is not used"

我有一个快速的问题,关于我正在工作的c#项目中从VisualStudio中的Resharper收到的警告。警告是:"ReturnValueofpuremethodisnotused"发生这种情况的方法如下:privatestaticboolFilePathHasInvalidChars(stringuserInputPath){try{//thisiswherethewarningoccurs:Path.GetFullPath(userInputPath);}catch(Exceptione){Log.Error(String.Format("TheProgramfailedtorun

c# - 如何初始化 ConcurrentDictionary?错误 : "Cannot access private method ' Add' here"

我有一个静态类,我在其中使用字典作为查找表以在.NET类型和SQL类型之间进行映射。这是一个这样的字典的例子:privatestaticreadonlyDictionarySqlServerMap=newDictionary{{typeof(Boolean),"bit"},{typeof(Byte[]),"varbinary(max)"},{typeof(Double),"float"},{typeof(Byte),"tinyint"},{typeof(Int16),"smallint"},{typeof(Int32),"int"},{typeof(Int64),"bigint"},{

c# - CS1501 : No overload for method 'ToString' takes 0 arguments?

ToString的无重载怎么可能采用零参数?零参数ToString是System.Object的一部分!编辑以回应接近投票:由于我无法将我的构建服务器升级到.NET4.5,有什么方法可以使此代码与.NET4.0/VS2010编译器?没有给我的重载一个完全不同的名称,这并不酷。所讨论的对象是一个F#区分联合,它覆盖了从System.Object继承的ToString方法。被覆盖的ToString正在被属于同一解决方案的C#项目中的代码调用。这一切都很好,直到我将ToString的额外重载添加到我的可区分联合中,它接受一个参数。这一切都在我的本地机器上完美构建和运行(VS2012,所有项目

c# - "override"枚举的最佳方法是什么?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Enum“Inheritance”我有许多扩展抽象类的类。抽象父类定义了一个具有一组值的枚举。一些子类继承父类的枚举值,但一些子类需要枚举值不同。有什么方法可以以某种方式覆盖这些特定子类的枚举,如果没有,实现我所描述的内容的好方法是什么?classParentClass{privateMyEnumm_EnumVal;publicvirtualMyEnumEnumVal{get{returnm_EnumVal;}set{m_EnumVal=value;}}publicenumMyEnum{a,b,c};}cla

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# - 委托(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