草庐IT

Generics

全部标签

c# - 泛型:强制转换和值类型,为什么这是非法的?

为什么这是编译时错误?publicTCastToCastMe(TSourcei){return(TCastTo)i;}错误:Cannotconverttype'TSource'to'TCastTo'为什么这是一个运行时错误?publicTCastToCastMe(TSourcei){return(TCastTo)(object)i;}inta=4;longb=CastMe(a);//InvalidCastException//thiscontrivedexampleworksintaa=4;intbb=CastMe(aa);//thisalsoworks,theproblemislim

c# - 在 C# 中使用泛型时装箱

我有以下简单的C#代码:privateStackm_stack=newStack();publicvoidAdd(Tobj)whereT:Person{m_stack.Push(obj);}这将产生以下IL代码:.methodpublichidebysiginstancevoidAdd(!!Tobj)cilmanaged{//Codesize20(0x14).maxstack8IL_0000:nopIL_0001:ldarg.0IL_0002:ldfldclass[System]System.Collections.Generic.Stack`1ConsoleApplication1.

c# - 通过反射设置属性 Nullable<>

我尝试动态设置一个Nullable属性。我得到我的属性(property)ex:PropertyInfoproperty=class.GetProperty("PropertyName");//MypropertyisNullableatthistimeSothetypecouldbeastringorint我想像这样通过反射来设置我的属性property.SetValue(class,"1256",null);当我的属性是NullableGeneric时它不起作用。所以我试图找到一种方法来设置我的属性。了解我执行的可空属性的类型Nullable.GetUnderlyingType(p

C# 泛型 - 如何返回特定类型?

也许我做错了。我有一堆派生自“模型”类的类,这是一个具有大量通用属性和方法的基类。我希望它们都实现一组功能:publicabstractvoidCreate();publicabstractTRead(GuidID);//然后我在像“Appointment”这样的子类中实现它:publicoverrideTRead(GuidID){varappt=db.Appointments.First(a=>a.AppointmentID.Equals(ID));varappointment=newAppointment(){DateEnd=appt.dateEnd.GetValueOrDefau

c# - C# 中的多态性、重载和泛型

classPoly{publicstaticvoidWriteVal(inti){System.Console.Write("{0}\n",i);}publicstaticvoidWriteVal(strings){System.Console.Write("{0}\n",s);}}classGenWriter{publicstaticvoidWrite(Tx){Poly.WriteVal(x);}}为什么在C#中不接受无辜的(对于C++程序员)方法Write?您可以看到编译器尝试将参数类型T与具体重载相匹配before实例化:错误3Thebestoverloadedmethodmat

c# - 通用扩展方法歧义

我定义了两个接口(interface)://IVector.cspublicinterfaceIVector{intSize{get;}floatthis[intindex]{get;set;}}//IMatrix.cspublicinterfaceIMatrix{intSize{get;}floatthis[introw,intcolumn]{get;set;}}以及这些接口(interface)的扩展方法//VectorExtensions.cspublicstaticTAdd(thisTvector,Tvalue)whereT:struct,IVector{varoutput=d

c# - 如何调整 "is a type but is used like a variable"?

我正在尝试在网络服务中生成一些代码。但它返回了2个错误:1)List是一种类型,但像变量一样使用2)方法“Customer”没有重载接受“3个参数”[WebService(Namespace="http://tempuri.org/")][WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)][ToolboxItem(false)]publicclasswstest:System.Web.Services.WebService{[WebMethod]publicListGetList(){Listli=List();li.A

c# - 为什么我不能将 List<Customer> 作为参数传递给接受 List<object> 的方法?

下面的代码给我这个错误:Cannotconvertfrom'System.Collections.Generic.List'to'System.Collections.Generic.List'.我如何向编译器表明Customer确实继承自对象?或者它只是不对通用集合对象进行继承(发送List会得到相同的错误)。usingSystem.Collections.Generic;usingSystem.Windows;usingSystem.Windows.Documents;namespaceTestControl3423{publicpartialclassWindow2:Window

c# - 将列表<int> 转换为列表<long>

如何转换List至List在C#中? 最佳答案 像这样:Listlongs=ints.ConvertAll(i=>(long)i);这使用C#3.0lambda表达式;如果您在VS2005中使用C#2.0,则需要编写Listlongs=ints.ConvertAll(delegate(inti){return(long)i;}); 关于c#-将列表转换为列表,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.c

c# - IDictionary<string, string> 与 Dictionary<string, string>

在这里使用IDictionary的值(value)是什么? 最佳答案 使用接口(interface)的值(value)始终相同:切换到另一个后端实现时,您不必更改客户端代码。请考虑稍后分析您的代码表明哈希表实现(在Dictionary类中使用)不适合您的任务,二叉搜索树的性能会更好。如果您已经编码到一个接口(interface),那么切换实现就很简单了。但是,如果您使用的是具体类,则必须在更多地方更改更多代码。=>这会花费时间和金钱。 关于c#-IDictionary与Dictionar