C# 如果不为空,则添加到集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11668965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add to Collection if Not Null
提问by Aabela
I have a very large object with many nullable-type variables. I also have a dictionary which I want to fill up with this object's non-null variables.
我有一个非常大的对象,其中包含许多可为空类型的变量。我还有一个字典,我想用这个对象的非空变量来填充它。
The code will look something like this
代码看起来像这样
if (myObject.whatever != null)
{
myDictionary.Add("...",myObject.whatever);
}
if (myObject.somethingElse != null)
{
myDictionary.Add("...",myObject.somethingElse);
...
EDIT (Sorry messed up the code)
编辑(抱歉搞砸了代码)
When we repeat this for the umpteenth time we get a mess of very long code. Is there some shorter way I could write this mess? I know about the Conditional Operator (aka ?) but that's just for assignments. Is there something like that for adding to a collection?
当我们无数次重复这个时,我们会得到一堆很长的代码。有什么更短的方法可以写这个烂摊子吗?我知道条件运算符(又名?),但这仅用于赋值。有没有类似的东西可以添加到集合中?
采纳答案by Botz3000
How about an extension method for your dictionary?
你的字典的扩展方法怎么样?
public static void AddIfNotNull<T,U>(this Dictionary<T,U> dic, T key, U value)
where U : class {
if (value != null) { dic.Add(key, value); }
}
You could then do this:
然后你可以这样做:
myDictionary.AddIfNotNull("...",myObject.whatever);
回答by dasblinkenlight
You can make a method that hides your if:
您可以制作一个隐藏您的方法if:
AddIfNotNull(myDictionary, "...", myObject.whatever);
private static void AddIfNotNull<K,T>(
IDictionary<K,T> myDictionary
, K key
, T value) {
if (value != default(T)) {
myDictionary.Add(key, value);
}
}
You can earn some "points for style" by making the method an extension (you need to add it to a static class then):
您可以通过使该方法成为扩展来获得一些“风格点数”(然后您需要将其添加到静态类中):
private static void AddIfNotNull<K,T>(
this IDictionary<K,T> myDictionary
, K key
, T value) {
if (value != default(T)) {
myDictionary.Add(key, value);
}
}
myDictionary.AddIfNotNull(myDictionary, "...", myObject.whatever);
If you know that you are inserting only reference type objects, replace default(T)with nulland add a class Tconstraint to the generic.
如果你知道你将只引用类型的对象,更换default(T)与null和添加class T约束通用。
回答by Philipp
If it's not a performance problem: Why not add everything and then remove what you don't need?
如果不是性能问题:为什么不添加所有内容,然后删除不需要的内容?
回答by Shai
public void addToDict(string ?myObj, Dictionary<,> myDict) {
if (myObj != null)
myDict.Add("...", myObj);
}
addToDict(myObject.whatever, myDict);
addToDict(myObject.somethignElse, myDict);
etc
等等
回答by Tim S.
I'd recommend writing an extension method:
我建议编写一个扩展方法:
public static class MyExtensions
{
public static void AddIfNotNull<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if ((object)value != null)
dictionary.Add(key, value);
}
}
Using (object)value != nullensures that this works as you'd expect with nullable types, (e.g. int?) value types, (e.g. int) and reference types (e.g. SomeClass). If you compare it to default(TValue), then an intof 0will not be added, even though it's not null. If you include a TValue : classrequirement, you can't use Nullable<T>as the type, which it sounds like is your most common usage.
Using(object)value != null确保这对可为空类型(例如int?)值类型(例如int)和引用类型(例如SomeClass)如您所期望的那样工作。如果将其与 进行比较default(TValue),则不会添加intof 0,即使它不为空。如果您包含一个TValue : class需求,则不能将其Nullable<T>用作类型,这听起来像是您最常见的用法。

