CompareTo函数的行为不符合我在C#中的预期 [英] CompareTo function does not behave as I expected in C#

查看:82
本文介绍了CompareTo函数的行为不符合我在C#中的预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"01:00" .CompareTo(-1:00")

那应该是1,但是在C#中是-1.

有人可以告诉我为什么吗?否则,我根本就不敢使用它们.

"01:00".CompareTo("-1:00")

That is supposed to be 1, but it''s -1 in C#.

Can someone tell me why? Otherwise I can''t trust to use them at all.

推荐答案

您正在进行字符串比较.字符串比较以单词排序顺序"完成,这是一种与文化相关的事情,连字符被忽略.请参阅 MSDN [
You''re doing a string comparison. String comparisons are done in ''word sort order'', which is a culture dependent thing and hyphens are ignored. See MSDN[^].

You probably wanted a numeric or DateTime comparison. String comparisons are not generally a good idea.


这篇关于CompareTo函数的行为不符合我在C#中的预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆