草庐IT

Equality

全部标签

c# - TypeDelegator 平等不一致?

考虑以下代码:classMyType:TypeDelegator{publicMyType(Typeparent):base(parent){}}classProgram{staticvoidMain(string[]args){Typet1=typeof(string);Typet2=newMyType(typeof(string));Console.WriteLine(EqualityComparer.Default.Equals(t1,t2));//.Default.Equals(t2,t1));//为什么不同版本的Equals返回不同的结果?EqualityComparer.D

c# - 字符串相等与 null 处理

我会经常使用这段代码来比较一个字符串:if(!string.IsNullOrEmpty(str1)&&str1.Equals(str2)){//theyareequal,domything}这首先处理null情况等。是否有一种更简洁的方法来进行字符串比较,也许是使用一个方法调用来处理可能的空值?我只是想知道如果测试值为空,则字符串不相等。(我有似曾相识的感觉,我以前可能问过这个问题,如果是这样,我深表歉意)更新:在我的例子中,str2是一个已知的比较好的字符串,所以我不需要检查它是否为null。str1是可能为null的“未知”字符串,所以在str1是null的情况下我想说“str1不

c# - 字符串相等与 null 处理

我会经常使用这段代码来比较一个字符串:if(!string.IsNullOrEmpty(str1)&&str1.Equals(str2)){//theyareequal,domything}这首先处理null情况等。是否有一种更简洁的方法来进行字符串比较,也许是使用一个方法调用来处理可能的空值?我只是想知道如果测试值为空,则字符串不相等。(我有似曾相识的感觉,我以前可能问过这个问题,如果是这样,我深表歉意)更新:在我的例子中,str2是一个已知的比较好的字符串,所以我不需要检查它是否为null。str1是可能为null的“未知”字符串,所以在str1是null的情况下我想说“str1不

c# - 元组与字符串作为 C# 中的字典键

我有一个使用ConcurrentDictionary实现的缓存,我需要保留的数据取决于5个参数。所以从缓存中获取的方法是:(为了简单起见,这里只显示了3个参数,为了清楚起见,我将数据类型改为代表CarData)publicCarDataGetCarData(stringcarModel,stringengineType,intyear);我想知道在我的ConcurrentDictionary中使用什么类型的键会更好,我可以这样做:varcarCache=newConcurrentDictionary();//checkforcarkeyboolexists=carCache.Conta

c# - 元组与字符串作为 C# 中的字典键

我有一个使用ConcurrentDictionary实现的缓存,我需要保留的数据取决于5个参数。所以从缓存中获取的方法是:(为了简单起见,这里只显示了3个参数,为了清楚起见,我将数据类型改为代表CarData)publicCarDataGetCarData(stringcarModel,stringengineType,intyear);我想知道在我的ConcurrentDictionary中使用什么类型的键会更好,我可以这样做:varcarCache=newConcurrentDictionary();//checkforcarkeyboolexists=carCache.Conta

c# - 与 Double.NaN 相等

我有以下代码...if(Price_Foreign!=Double.NaN){output.Append(spacer);output.Append(String.Format("{0,-10:C}USD",Price_Foreign));}哪些输出:NaNUSD什么给了?我正在使用Double.NaN来指示该值不存在,不应输出。 最佳答案 也许您正在寻找IsNaN静态函数?尝试这样的事情:if(!Double.IsNaN(Price_Foreign)){output.Append(spacer);output.Append(Str

c# - 与 Double.NaN 相等

我有以下代码...if(Price_Foreign!=Double.NaN){output.Append(spacer);output.Append(String.Format("{0,-10:C}USD",Price_Foreign));}哪些输出:NaNUSD什么给了?我正在使用Double.NaN来指示该值不存在,不应输出。 最佳答案 也许您正在寻找IsNaN静态函数?尝试这样的事情:if(!Double.IsNaN(Price_Foreign)){output.Append(spacer);output.Append(Str

c# - 使用 == 比较两个结构

我正在尝试在C#中使用等号(==)比较两个结构。我的结构如下:publicstructCisSettings:IEquatable{publicintGain{get;privateset;}publicintOffset{get;privateset;}publicintBright{get;privateset;}publicintContrast{get;privateset;}publicCisSettings(intgain,intoffset,intbright,intcontrast):this(){Gain=gain;Offset=offset;Bright=brigh

c# - 使用 == 比较两个结构

我正在尝试在C#中使用等号(==)比较两个结构。我的结构如下:publicstructCisSettings:IEquatable{publicintGain{get;privateset;}publicintOffset{get;privateset;}publicintBright{get;privateset;}publicintContrast{get;privateset;}publicCisSettings(intgain,intoffset,intbright,intcontrast):this(){Gain=gain;Offset=offset;Bright=brigh

c# - 比较两个 List<string> 是否相等

除了逐一检查元素之外,我如何比较两个字符串列表是否相等(在.NET3.0中):这失败了://Expectedresult.Listexpected=newList();expected.Add("a");expected.Add("b");expected.Add("c");//Actualresultactual=newList();actual.Add("a");actual.Add("b");actual.Add("c");//VerdictAssert.IsTrue(actual==expected); 最佳答案 尝试以下操