【c# – tasks参数包含空值】教程文章相关的互联网学习教程文章

c# – 处理空值【代码】

今天调试我的代码时,我有一个奇怪的情况. 正如屏幕截图所示,我有一个对象obj从System.Data.OracleClient ExecuteScalar()中产生一个值. 将得到的值分配给变量obj,它清楚地显示了它的null,但是执行已经进入if(obj!= null)语句,这在技术上不应该.有什么想法吗?我想知道我的代码是否正确?解决方法:请尝试使用DBNull.Valueif(obj != DBNull.Value)

c# – 这是使用泛型检查空值的最佳方法吗?【代码】

public static T IsNull<T>(object value, T defaultValue) {return ((Object.Equals(value,null)) | (Object.Equals(value,DBNull.Value)) ?defaultValue : (T)value); }public static T IsNull<T>(object value) where T :new() {T defaultvalue = new T();return IsNull(value, defaultvalue); } 已经过测试,可以用来对付数据对象,类和变量.只是想知道是否有更好的方法来解决这个问题.解决方法:看起来你正试图复制null coalesce...

c# – 通过使用LINQ跳过空值将Dictionary转换为Dictionary【代码】

我有以下产品类:public class Product {public string Name { get; set; }public float Price { get; set; } public int? CategoryId { get; set; } }现在我必须计算每个CategoryId的产品数量,并将它们放在Dictionary< int,int>中.因此:IQueryable<Product> products = _productServices.GetAll(); //return IQueryable<Product>Dictionary<int, int> productDict = products.ToList().GroupBy(p => p.CategoryId).ToDictio...

c# – 用于空值的正当例外【代码】

在以下示例中,我们有两个不同的异常要通信.//constructor public Main(string arg){if(arg==null)throw new ArgumentNullException("arg");Thing foo=GetFoo(arg);if(foo==null)throw new NullReferenceException("foo is null"); }这是两种异常类型的正确方法吗?解决方法:第一个例外肯定是正确的.这是第二个棘手的问题. 这里有两种可能性: > GetFoo()并不意味着永远返回null.在那种情况下,我们基本上证明了GetFoo()中的一个...

c# – 关于检查空值的问题【代码】

我和我的一位同事讨论了检查空值的问题. 他认为“在某些情况下”下面的代码会给他一个空值异常:string test = null; if(test == null) //error here {}但如果将代码更改为此,则不会出现错误:string test = null; if(null == test) //NO error here {}我告诉他这不可能发生,但他发誓修复了他的代码.是否有任何可能导致上述更改可能导致错误的情况?解决方法:没有字符串,没有.你可以用写得不好的==重载来做到这一点:using System;...

c# – List.Sort错误地排序空值【代码】

我有一个对象列表,其中一些可以为null.我希望它按一些属性排序,列表末尾有空值.但是,无论比较器返回什么,List< T> .Sort()方法似乎都会在开头放置空值.这是我用来测试这个的一个小程序:class Program{class Foo {public int Bar;public Foo(int bar){Bar = bar;}}static void Main(string[] args){List<Foo> list = new List<Foo>{null, new Foo(1), new Foo(3), null, new Foo(100)};foreach (var foo in list){Console.WriteLin...

c# – 具有“out”参数的类似TryXXX的方法与返回可空值类型?【代码】

我经常在C#中看到这样的方法:(做一个可能给出或不给出结果的计算)bool TrySomething(SomeType inputData, out SomeOtherType result) { ... }为什么人们不使用这样的东西呢?Nullable<SomeOtherType> TrySomething(SomeType inputData) { ... }这只是性能差异吗?这是一个结构,所以必须没有堆分配,对吧?或者我错过了什么?解决方法:在C#2.0中引入了Nullable – 就像泛型一样.有很多代码早于此. 说实话,每当出现问题时,我都不会只...

c# – 如何在二进制搜索期间处理空值?【代码】

在List< string>的二进制搜索期间处理空值的最佳方法是什么? (好吧,如果我能事先读出所有的值,它将是一个List< string>)?int previous = 0; int direction = -1; if (itemToCompare == null) {previous = mid;for (int tries = 0; tries < 2; tries++) {mid += direction;itemToCompare = GetItem(mid);while (itemToCompare == null && insideInclusiveRange(min, max, mid)) {mid += direction;itemToCompare = GetItem(mid);}...

c# – 无法使用空值序列化json【代码】

[JsonObject(MemberSerialization.OptIn)] public class NameIDItem {[JsonProperty]public string _name{ get; set; }[JsonProperty]public int _id { get; set; }public NameIDItem(){}public NameIDItem(string name, int id){_name = name;_id = id;} }[JsonObject(MemberSerialization.OptIn)] public class LinkListOfUserGroup {public List<NameIDItem> NewItems { get; set; }public List<NameIDItem> RemovedItems { get...

c# – 考虑到语言环境,是否可以在LINQ查询中为空值进行回退?【代码】

我正在创建一个C#.NET 3.5网站.在本网站中,从XML文件中读取一些内容.这个XML文件是从Excel文件中保存的,所以基本上Excel是一种内容管理系统(在这种情况下我不能使用数据库).我正在使用LINQ to XML来获取内容对象的集合,以便我可以在webforms上使用它们.我刚开始使用LINQ,但我知道分组和排序等基础知识. 我使用以下代码将内容读入集合(在本例中为视频列表):string path = String.Format(server.MapPath("~/App_Data/Content.{0}.xm...

c# – ExpandoObjects的动态视图“隐藏”具有空值的属性【代码】

我有一些代码适用于由数据库调用填充的ExpandoObjects.总是有些值是空值.当我将对象看作ExpandoObject时,我会看到底层字典中的所有键和值(包括空值).但是,如果我尝试通过动态引用访问它们,则任何具有相应空值的键都不会显示在对象的动态视图中.当我尝试通过动态引用上的属性语法访问它时,我得到一个ArgumentNullException. 我知道我可以通过直接使用ExpandoObject,添加一堆try catch,将expando映射到具体类型等来解决这个问题,但这...

c# – 如何为pinvoke调用的结构分配空值?【代码】

参见英文答案 > C#: How to pass null to a function expecting a ref? 6个我想调用LsaOpenPolicy,它采用LSA_OBJECT_ATTRIBUTES结构.我正在使用the struct definition from pinvoke.net.此结构具有字段public LSA_UNICODE_STRING ObjectName;. LSA_OBJECT_ATTRIBUTES MSDN文章说:When you call LsaOpenPolicy, initialize the members of this structure to NULL or zero because the functio...

C# – 如何判断DataColumn是否支持空值?【代码】

我有一个来自SQL请求的数据表.虽然我真的在使用OLEDB对着一个表,但即使我从我的SQL服务器获取表,我也有同样的问题. 如果我填充数据表然后查询DataColumns – 他们都说AllowDBNull == true和allowNull == true.但是,如果我看一下SSMS中的表格,它会另有说明.string selectStmt= "Select * from foobar; " DataSet NewData = new DataSet(); using (SqlConnection DataConn = new SqlConnection(MyConnectionString)) { SqlDataAd...

c# – 在Serilog中进行解构时忽略空值【代码】

在Serilog中,当值为null时是否可以忽略属性? 我已经尝试添加DataMember属性但这些似乎被忽略了:[DataMember(EmitDefaultValue = false)] public string Title { get; set; }进一步查看源代码我注意到有一个AttributedDestructuringPolicy,但这似乎控制渲染属性值而不是省略属性.解决方法:这将是AttributedDestructuringPolicy的一个很好的补充 – 我认为目前没有任何内置功能可以启用它.

在C#中实现GetHashCode.空值处理【代码】

在开始之前,我在Mono环境中测试了所有代码示例,并且GetHashCode实现中有一个明显的区别:string.Empty.GetHashCode(); // returns 0 in Mono 3.10 string.Empty.GetHashCode(); // returns 757602046 in .NET 4.5.1我通过@JonSkeet基于这个SO Answer实现了我的实现,并且在评论中他还建议使用0哈希值来表示NULL值(不知道我应该如何哈希它们).I usually use 0 as the effective hash code for null – which isn’t the same as igno...