草庐IT

default-implementation

全部标签

c# - default(T) 使用空集合而不是 null

我想要一个通用方法,它为传递的类型返回默认值,但是对于集合类型,我想要得到空集合而不是null,例如:GetDefault();//returnsemptyarrayofint'sGetDefault();//returns0GetDefault();//returnsnullGetDefault>();//returnsemptylistofobjects我开始写的方法如下:publicstaticTGetDefault(){vartype=typeof(T);if(type.GetInterface("IEnumerable")!=null)){//returnemptycolle

C# 功能请求 : implement interfaces on anonymous types

我想知道做这样的事情需要什么:usingSystem;classProgram{staticvoidMain(){varf=newIFoo{Foo="foo",Print=()=>Console.WriteLine(Foo)};}}interfaceIFoo{StringFoo{get;set;}voidPrint();}创建的匿名类型看起来像这样:internalsealedclassf__AnonymousType0j__TPar>:IFoo{readonlyj__TPari__Field;publicf__AnonymousType0(j__TParFoo){this.i__Fi

c# - 如何更改 C# 中的 default(T) 返回值?

我想更改default(T)对某些类的行为方式。因此,我不想为我的引用类型返回null,而是返回一个null对象。有点像kids.Clear();varkid=kids.Where(k=>k.Age有人知道这是否可能吗? 最佳答案 Anyoneknowifthisisatallpossible?根本不可能。但也许您想使用DefaultIfEmpty相反:kids.Clear();varkid=kids.Where(k=>k.Age 关于c#-如何更改C#中的default(T)返回值?,

c# - 如何在 C# 中覆盖 default(T)?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:HowtochangewhatDefault(T)returnsinC#print(default(int)==0)//true同样,如果我有一个自定义对象,它的默认值将为null。print(default(Foo)==null)//true我可以为default(Foo)设置一个自定义值而不是null吗?例如,像这样:publicstaticoverrideFoodefault(){returnnewFoo();}这不会编译。谢谢..

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

c# - Properties.Settings.Default.Save(); ->那个文件在哪里

我有一个使用“设置”的应用。要保存我使用的设置:Properties.Settings.Default.Save();阅读我使用的:Properties.Settings.Default.MyCustomSetting;在我的应用程序文件夹中,我只有exe文件。没有配置文件。我的应用程序运行良好,可以读写设置。如果该文件不在应用程序文件夹中,该文件位于何处? 最佳答案 在我的WindowsXP机器上,设置保存在C:\DocumentsandSettings\\ApplicationData\下某处名为user.config的文件中。

c# - 如何判断用户访问的是 "/Default.aspx"还是 "/"

我正在我的Global.asax.cs的Application_BeginRequest部分中编写。出于SEO目的,我正在尝试重定向正在查看的用户:http://www.example.com/Default.aspx到:http://www.example.com/我的问题是:我如何知道用户正在看哪个?我一直在使用:HttpContext.Current.Request.Url.*但是无论我访问哪一个,所有参数都是相同的。 最佳答案 您可以获取在用户浏览器中输入的路径:stringpath=Request.RawUrl;MSDN

c# - 为什么 List<T>.Sort 使用 Comparer<int>.Default 比等效的自定义比较器快两倍以上?

结果使用1000万个随机列表ints(每次相同的种子,重复10次的平均值):listCopy.Sort(Comparer.Default)需要314毫秒。使用sealedclassIntComparer:IComparer{publicintCompare(intx,inty){returnxlistCopy.Sort(newIntComparer())需要716毫秒。一些变化:使用structIntComparer而不是sealedclass:771毫秒使用publicintCompare(intx,inty){returnx.CompareTo(y);}:809毫秒评论Compar

c# - 在 switch case 中,如果我们将 "default"写为任何单词或单个字母,它不会抛出错误

在switch中,如果我们写任何单词或单个字母而不是default,它不会抛出错误。例如switch(10){case1:break;hello:break;}它运行时没有抛出错误。谁能解释一下这是如何工作的? 最佳答案 它正在编译,因为hello:是一个标签,因此可以作为goto的目的地。当我编译这个时,我收到了关于未引用标签的警告(因为我没有转到)这是您可以放入LINQPad的示例-您会注意到它同时打印“1”和“hello”:switch(1){case1:"1".Dump();gotohello;break;hello:"he

c# - 如何解决错误 :the type does not appear to implement microsoft. practices.servicelocation.iservicelocator?

我是MVC的新手,我正在关注“AdamFreeman的PROASP.NETMVC4”。我目前正在研究它的第6章。我正在学习如何使用MVC4中的Ninject进行依赖注入(inject)。我已经按照书中的描述创建了应用程序。现在我不明白为什么会出现以下错误:该类型似乎没有实现microsoft.practices.servicelocation.iservicelocator这是我的Controller代码:publicclassHomeController:Controller{privateProduct[]products={newProduct{Name="Kayak",Cate