相关疑难解决方法(0)

为什么不将异常用作常规控制流?

为了避免我可以用Google搜索的所有标准答案,我将提供一个你可以随意攻击的例子.

C#和Java(和太多的人)有很多类型的一些"溢出"的行为,我不都不像(如type.MaxValue + type.SmallestValue == type.MinValue例如: int.MaxValue + 1 == int.MinValue).

但是,看到我的恶性,我会通过扩展这种行为来增加对这种伤害的一些侮辱,让我们说一个Overridden DateTime类型.(我知道DateTime密封在.NET中,但是为了这个例子,我使用的是一种与C#完全相同的伪语言,除了DateTime没有密封的事实).

被覆盖的Add方法:

/// <summary>
/// Increments this date with a timespan, but loops when
/// the maximum value for datetime is exceeded.
/// </summary>
/// <param name="ts">The timespan to (try to) add</param>
/// <returns>The Date, incremented with the given timespan. 
/// If DateTime.MaxValue is exceeded, the sum wil 'overflow' and 
/// continue from DateTime.MinValue. 
/// </returns>
public DateTime override Add(TimeSpan …
Run Code Online (Sandbox Code Playgroud)

language-agnostic exception

184
推荐指数
12
解决办法
4万
查看次数

合并两个字典对c#not pure concat

我有两个字典.

Dictionary<string, string> testDict = new Dictionary<string, string>();
            testDict.Add("Name", "John");
            testDict.Add("City", "NY");

Dictionary<string, string> DictA = new Dictionary<string, string>();
            DictA.Add("Name", "Sarah");
            DictA.Add("State", "ON");
Run Code Online (Sandbox Code Playgroud)

我希望得到一个字典,以便testDict的键存在,并且存在DictA中存在的那些键的值.

因此合并字典的示例应如下所示:

Dictionary<string, string> DictMerged = new Dictionary<string, string>();
                DictMerged.Add("Name", "Sarah");
                DictMerged.Add("City", "NY");
Run Code Online (Sandbox Code Playgroud)

我希望我能够解释我的要求..

我试过了..

testDict.Concat(DictA)
  .GroupBy(kvp => kvp.Key, kvp => kvp.Value)
  .ToDictionary(g => g.Key, g => g.Last());
Run Code Online (Sandbox Code Playgroud)

但这给了我DictA'State',我不想要......

任何帮助都是真诚的感谢

谢谢

.net c# linq dictionary

1
推荐指数
1
解决办法
197
查看次数

标签 统计

.net ×1

c# ×1

dictionary ×1

exception ×1

language-agnostic ×1

linq ×1