草庐IT

audio_hw_generic

全部标签

C# 泛型 : what's the point of the "X<T> where T: X<T>" generic type constraint?

读一本书:NHibernate3:Beginnersguide我发现了一个让我很好奇的片段:Timeforaction–Creatingabaseentity(...)AddanewclasstothefolderDomainoftheprojectandcallitEntity.MaketheclassabstractandgenericinT.Yourcodeshouldlooksimilartothefollowingcodesnippet:usingSystem;namespaceOrderingSystem.Domain{publicabstractclassEntitywh

c# - Generic 类型参数前的 "out"是什么意思?

我刚刚在寻找GroupBy返回类型时看到了一个不熟悉的语法:publicinterfaceIGrouping:IEnumerableMSDNSource我知道out在方法中是什么意思,但在泛型接口(interface)中不知道。out在泛型中是什么意思? 最佳答案 它表示一个covariant范围。另见关于MSDN的描述.本质上它说,IGrouping可以看作是IGrouping,因此你可以IGroupinggr=MakeGrouping(...);IGroupinggrBase=gr;如果Aderived是从Abase派生的接口(

c# - 没有从 'System.Collections.Generic.List<T>' 到 'T' 的隐式引用转换

classClass1{publicvirtualvoidUpdate(Tentity){Update(newList(){entity});//It'sfailed}publicvirtualvoidUpdate(IEnumerableentities){}publicvirtualvoidUpdate(TSubentity)whereTSub:T{}publicvirtualvoidUpdate(IEnumerableentities)whereTSub:T{}}我有一段代码。但它总是失败。如果我替换了Update(newList(){entity})通过Update((newLi

C# LINQ to SQL : Refactoring this Generic GetByID method

我写了下面的方法。publicTGetByID(intid){vardbcontext=DB;vartable=dbcontext.GetTable();returntable.ToList().SingleOrDefault(e=>Convert.ToInt16(e.GetType().GetProperties().First().GetValue(e,null))==id);}基本上它是通用类中的一个方法,其中T是DataContext中的一个类。该方法从T的类型(GetTable)获取表格,并检查输入参数的第一个属性(始终是ID)。问题是我必须先将元素表转换为列表才能对属性执行

c# - "Base abstract generic class is a bad choice in most situations."为什么? (或者为什么不)

我刚刚看到关于blog的评论帖子:Baseabstractgenericclassisabadchoiceinmostsituations这是真的吗?如果不是,为什么?得出此声明的见解是什么? 最佳答案 我同意,因为任何继承抽象泛型类的东西都不会与基类多态。也就是说,如果你有abstractclassmyBase然后你创建classmyThing:myBaseclassmyOtherThing:myBase您不能创建针对myThing和myOtherThing的方法,因为它们不共享祖先。基类是抽象的没有意义,真的,它还不如只是一个类

C# Language : generics, open/closed, bound/unbound, constructed

我正在阅读AndersHejlsberg等人撰写的《C#编程语言》第4版。有几个定义有点曲折:未绑定(bind)的泛型类型:泛型类型声明本身表示未绑定(bind)的泛型类型......构造类型:至少包含一个类型参数的类型称为构造类型。开放类型:开放类型是涉及类型参数的类型。封闭类型:封闭类型是一种非开放类型。未绑定(bind)类型:指非泛型类型或未绑定(bind)泛型。绑定(bind)类型:指非泛型类型或构造类型。[注释]ERICLIPPERT:是的,非泛型类型被认为是绑定(bind)和未绑定(bind)的。问题1,下面我列出的是正确的吗?int//non-generic,closed

c# - 无法将类型 'System.Collections.Generic.IEnumerable<AnonymousType#1>' 隐式转换为 'System.Collections.Generic.List<string>

我有以下代码:Listaa=(fromcharcinsourceselectnew{Data=c.ToString()}).ToList();但是呢Listaa=(fromcharc1insourcefromcharc2insourceselectnew{Data=string.Concat(c1,".",c2)).ToList();编译出错Cannotimplicitlyconverttype'System.Collections.Generic.List'to'System.Collections.Generic.List'需要帮助。 最佳答案

c# - 将语句与泛型一起使用 : using ISet<> = System. Collections.Generic.ISet<>

因为我使用了两个不同的通用集合命名空间(System.Collections.Generic和Iesi.Collections.Generic),所以我有冲突。在项目的其他部分,我同时使用nunit和mstest框架,但是当我调用Assert时,我想通过使用nunit版本usingAssert=NUnit.Framework.Assert;效果很好,但我想对泛型类型做同样的事情。但是,以下几行不起作用usingISet=System.Collections.Generic.ISet;usingISet=System.Collections.Generic.ISet;有谁知道如何告诉.n

c# - 在 C# 中初始化一个 Generic.List

在C#中,我可以使用以下语法初始化列表。ListintList=newList(){1,2,3};我想知道{}语法是如何工作的,以及它是否有名称。有一个采用IEnumerable的构造函数,您可以调用它。ListintList=newList(newint[]{1,2,3});这似乎更“标准”。当我解构列表的默认构造函数时,我只看到this._items=Array.Empty;我希望能够做到这一点。CustomClassabc=newCustomClass(){1,2,3};并能够使用1,2,3列表。这是如何工作的?更新乔恩斯基特回答It'scallingtheparameterle

c# - Generic List<T> 作为方法的参数

如何使用List作为方法的参数,我尝试了这种语法:voidExport(Listdata,paramsstring[]parameters){}编译错误:Thetypeornamespacename'T'couldnotbefound(areyoumissingausingdirectiveoranassemblyreference?) 最佳答案 取一个通用的List与界限List您还需要使该方法通用。这是通过将通用参数添加到方法来完成的,这与将其添加到类型的方式非常相似。尝试以下操作voidExport(Listdata,para