草庐IT

non-struct

全部标签

c# - 为什么不能将 IEnumerable<struct> 转换为 IEnumerable<object>?

为什么不允许最后一行?IEnumerabledoubleenumerable=newList{1,2};IEnumerablestringenumerable=newList{"a","b"};IEnumerableobjects1=stringenumerable;//OKIEnumerableobjects2=doubleenumerable;//Notallowed这是因为double不是从对象派生的值类型,因此协方差不起作用吗?这是否意味着没有办法使这项工作:publicinterfaceIMyInterface{stringMethod();}publicclassMyCla

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# - 在 C# 中,List<struct> 中的值是否已装箱?

假设我声明了一个包含结构类型值的通用列表:structMyStruct{publicMyStruct(intval1,decimalval2):this(){Val1=val1;Val2=val2;}publicintVal1{get;privateset;}publicdecimalVal2{get;privateset;}}Listlist;List是否将每个单独的值存储为一个装箱结构,在堆上单独分配?还是比这更聪明? 最佳答案 没有装箱。"No,therewillbenoboxing.Thatwasoneofthemainde

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# - MVC : The parameters dictionary contains a null entry for parameter 'k' of non-nullable type 'System.Int32'

我是MVC的新手。在我的应用程序中,我正在从Mydatabase中检索数据。但是当我运行我的应用程序时,它会显示这样的错误这是我的网址http://localhost:7317/Employee/DetailsData/4ExceptionDetails:System.ArgumentException:Theparametersdictionarycontainsanullentryforparameter'k'ofnon-nullabletype'System.Int32'formethod'System.Web.Mvc.ActionResultDetailsData(Int32)

c# - 两个 C# 扩展泛型方法之间的调用不明确,其中 T :class and other where T:struct

考虑两种扩展方法:publicstaticTMyExtension(thisTo)whereT:classpublicstaticTMyExtension(thisTo)whereT:struct还有一个类:classMyClass(){...}现在在上述类的实例上调用扩展方法:varo=newMyClass(...);o.MyExtension();//compilererrorhere..o.MyExtension();//triedthisaswell-stillcompilererror..当我在一个类上调用它时,编译器说调用该方法是一个不明确的调用。我本以为它可以确定调用哪个

c# - 如何根据定义使 'struct' 可空?

structAccountInfo{StringUsername;StringPassword;}现在如果我想要一个Nullable例如我应该写:NullablemyAccount=null;但我想制作structNullable从本质上讲,它可以像这样使用(不使用Nullable):AccountInfomyAccount=null; 最佳答案 你不能。结构被视为值类型,根据定义不能为空。使其成为nullable的最简单方法是使其成为引用类型。您需要问自己的答案是“为什么这是一个结构?”除非你能想到一个真正可靠的理由,否则不要,并

c# - 返回两个值,元组 vs 'out' vs 'struct'

考虑一个返回两个值的函数。我们可以这样写://Usingout:stringMyFunction(stringinput,outintcount)//UsingTupleclass:TupleMyFunction(stringinput)//Usingstruct:MyStructMyFunction(stringinput)哪个是最佳实践,为什么? 最佳答案 它们各有优缺点。Out参数快速且便宜,但需要您传入一个变量,并依赖于变异。在LINQ中正确使用输出参数几乎是不可能的。元组会产生收集压力1并且不会self记录。“Item1”

c# - 为什么我会收到消息 "Invalid setup on a non-virtual (overridable in VB) member..."的异常?

我有一个单元测试,我必须模拟一个返回bool类型的非虚拟方法publicclassXmlCupboardAccess{publicboolIsDataEntityInXmlCupboard(stringdataId,outstringnameInCupboard,outstringrefTypeInCupboard,stringnameTemplate=null){returnIsDataEntityInXmlCupboard(_theDb,dataId,outnameInCupboard,outrefTypeInCupboard,nameTemplate);}}所以我有一个XmlCu

c# - 如何在 C# 中将 struct System.Byte byte[] 转换为 System.IO.Stream 对象?

如何转换结构System.Bytebyte[]到System.IO.StreamC#中的对象? 最佳答案 将字节数组转换为流的最简单方法是使用MemoryStream类:Streamstream=newMemoryStream(byteArray); 关于c#-如何在C#中将structSystem.Bytebyte[]转换为System.IO.Stream对象?,我们在StackOverflow上找到一个类似的问题: https://stackoverflo