Tag: C#的

c#字典:通过声明使关键字不区分大小写

我有一个Dictionary<string, object>字典。 它曾经是Dictionary<Guid, object>但其他的“标识符”已经发挥作用,现在键被处理为string。 问题是我的源数据中的Guid键是作为VarChar ,所以现在"923D81A0-7B71-438d-8160-A524EA7EFA5E"的键与"923d81a0-7b71-438d-8160-a524ea7efa5e" (wasn使用Guids时不会出现问题)。 关于.NET框架的真正好(和甜)是我可以这样做: Dictionary<string, CustomClass> _recordSet = new Dictionary<string, CustomClass>( StringComparer.InvariantCultureIgnoreCase); 那效果很好。 但是,嵌套的字典呢? 如下所示: Dictionary<int, Dictionary<string, CustomClass>> _customRecordSet = new Dictionary<int, Dictionary<string, CustomClass>>(); 我将如何指定这样的嵌套字典string比较器?

为什么不是`printf`中定义的`float`的说明符?

它看起来可能是,至less在C99中可以应用于int长度修饰符: %hhd , %hd , %ld和%lld表示有signed char , short , long和long long 。 甚至还有一个适用于double的长度修饰符: %Lf表示long double %Lf 。 问题是为什么他们忽略float ? 遵循这个模式,它可能是%hf 。

你如何使用逐字string插值?

在C#6中有一个新特性:插入string。 这些让你把expression式直接放到代码中,而不是依靠索引: string s = string.Format("Adding \"{0}\" and {1} to foobar.", x, this.Y()); 变为: string s = $"Adding \"{x}\" and {this.Y()} to foobar."; 然而,我们有很多使用逐字串(主要是SQL语句)的多行string: string s = string.Format(@"Result… Adding ""{0}"" and {1} to foobar: {2}", x, this.Y(), x.GetLog()); 将这些恢复为常规string似乎很麻烦: string s = "Result…\r\n" + $"Adding \"{x}\" and {this.Y()} to foobar:\r\n" + x.GetLog().ToString(); 我如何同时使用逐字string和插入string?

C#将函数存储在Dictionary中

如何创build一个可以存储函数的字典? 谢谢。 我有大约30多个可以从用户执行的function。 我想能够这样执行function: private void functionName(arg1, arg2, arg3) { // code } dictionaryName.add("doSomething", functionName); private void interceptCommand(string command) { foreach ( var cmd in dictionaryName ) { if ( cmd.Key.Equals(command) ) { cmd.Value.Invoke(); } } } 然而,函数签名并不总是相同的,因此具有不同数量的参数。

如何设置std :: vector的初始大小?

我有vector<CustomClass*> ,我把很多东西放在向量中,我需要快速访问,所以我不使用列表。 如何设置vector的初始大小(例如,为20000个位置,所以当我插入新的时候避免复制)?

方法不能转换成商店expression式

我看到这个代码与LINQ to SQL的工作,但是当我使用entity framework,它会引发这个错误: LINQ to Entities不能识别方法'System.Linq.IQueryable'1 [MyProject.Models.CommunityFeatures] GetCommunityFeatures()'方法,并且此方法不能被转换成存储expression式。 存储库代码是这样的: public IQueryable<Models.Estate> GetEstates() { return from e in entity.Estates let AllCommFeat = GetCommunityFeatures() let AllHomeFeat = GetHomeFeatures() select new Models.Estate { EstateId = e.EstateId, AllHomeFeatures = new LazyList<HomeFeatures>(AllHomeFeat), AllCommunityFeatures = new LazyList<CommunityFeatures>(AllCommFeat) }; } public IQueryable<Models.CommunityFeatures> GetCommunityFeatures() { return from f in entity.CommunityFeatures select new […]

ASP.NET MVC:隐藏的字段值不会使用HtmlHelper.Hidden呈现

我的应用程序发生了一些非常奇怪的事情: 我在ViewModel中有以下属性: public int? StakeholderId { get; set; } 它在部分视图中呈现如下: <%= Html.Hidden("StakeholderId", Model.StakeholderId) %> 表单被提交,并且相关的控制器动作生成一个id并更新模型,然后返回与更新的模型相同的视图 我遇到的问题是,即使StakeholderId现在有值,隐藏的字段也没有任何第二次呈现的“value”属性。 如果我只是自己输出值,它会显示在页面上,所以我通过这样做来显示值: <input type="hidden" id="StakeholderId" name="stakeholderId" value="<%: Model.StakeholderId %>" /> 但是很奇怪,助手没有拿起更新的值? (我使用jQuery提交表单并将操作结果呈现为div,但是我已经检查过,并且在jQuery执行任何操作之前,我已经得到的html已经是错误的了,所以我不认为这跟这个有很大关系任何东西) UPDATE 我已经发现,我也可以在我的控制器操作返回部分视图之前清除相关的ModelState键。

我在哪里可以了解MEF?

我和Glenn Block一起看了DNR电视剧集,看起来MEF对我的公司很有用。 我正在试图找出更多关于它的优点和缺点的信息以及一些使用它的示例项目。 有什么好的博客/教程使用MEF? 注:我使用C#,所以如果在C#中的例子,这将是很棒的。

是否所有的虚函数都需要在派生类中实现?

这可能看起来像一个简单的问题,但我无法在其他地方find答案。 假设我有以下几点: class Abstract { public: virtual void foo() = 0; virtual void bar(); } class Derived : Abstract { public: virtual void foo(); } Derived类不能实现bar()函数吗? 如果不是我所有的派生类都需要bar()函数,但是有一些呢。 所有的抽象基类的虚函数都需要在派生类中实现,还是仅仅是纯虚拟的? 谢谢

如何在C#中获取正确的时间戳

我想在我的应用程序中得到有效的时间戳,所以我写道: public static String GetTimestamp(DateTime value) { return value.ToString("yyyyMMddHHmmssffff"); } …later on in the code String timeStamp = GetTimestamp(new DateTime()); Console.WriteLine(timeStamp); 输出: 000101010000000000 我想要这样的东西: 20140112180244 我做错了什么?