String.IComparable.CompareTo(Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
virtual int System.IComparable.CompareTo(System::Object ^ value) = IComparable::CompareTo;
int IComparable.CompareTo (object value);
abstract member System.IComparable.CompareTo : obj -> int
override this.System.IComparable.CompareTo : obj -> int
Function CompareTo (value As Object) As Integer Implements IComparable.CompareTo
参数
返回
一个 32 位带符号整数,该整数指示此实例在排序顺序中是位于 value
参数之前、之后还是与其出现在同一位置。
“值” | 条件 |
---|---|
小于零 | 此实例位于 value 之前。 |
零 | 此实例在排序顺序中的位置与 value 相同。 |
大于零 | 此实例位于 value 之后,或 value 为 null 。 |
实现
例外
value
不是 String。
示例
以下示例将 方法与 一 CompareTo 起使用 Object。 由于它尝试将 实例与 对象进行比较String,因此 该方法会引发 ArgumentException。TestClass
using namespace System;
public ref class TestClass{};
int main()
{
TestClass^ test = gcnew TestClass;
array<Object^>^ objectsToCompare = { test, test->ToString(), 123,
(123).ToString(), "some text",
"Some Text" };
String^ s = "some text";
for each (Object^ objectToCompare in objectsToCompare) {
try {
Int32 i = s->CompareTo(objectToCompare);
Console::WriteLine("Comparing '{0}' with '{1}': {2}",
s, objectToCompare, i);
}
catch (ArgumentException^ e) {
Console::WriteLine("Bad argument: {0} (type {1})",
objectToCompare,
objectToCompare->GetType()->Name);
}
}
}
// The example displays the following output:
// Bad argument: TestClass (type TestClass)
// Comparing 'some text' with 'TestClass': -1
// Bad argument: 123 (type Int32)
// Comparing 'some text' with '123': 1
// Comparing 'some text' with 'some text': 0
// Comparing 'some text' with 'Some Text': -1
using System;
public class TestClass
{}
public class Example
{
public static void Main()
{
var test = new TestClass();
Object[] objectsToCompare = { test, test.ToString(), 123,
123.ToString(), "some text",
"Some Text" };
string s = "some text";
foreach (var objectToCompare in objectsToCompare) {
try {
int i = s.CompareTo(objectToCompare);
Console.WriteLine("Comparing '{0}' with '{1}': {2}",
s, objectToCompare, i);
}
catch (ArgumentException) {
Console.WriteLine("Bad argument: {0} (type {1})",
objectToCompare,
objectToCompare.GetType().Name);
}
}
}
}
// The example displays the following output:
// Bad argument: TestClass (type TestClass)
// Comparing 'some text' with 'TestClass': -1
// Bad argument: 123 (type Int32)
// Comparing 'some text' with '123': 1
// Comparing 'some text' with 'some text': 0
// Comparing 'some text' with 'Some Text': -1
open System
type TestClass() = class end
let test = TestClass()
let objectsToCompare: obj list =
[ test; string test; 123
string 123; "some text"
"Some Text" ]
let s = "some text"
for objectToCompare in objectsToCompare do
try
let i = s.CompareTo objectToCompare
printfn $"Comparing '{s}' with '{objectToCompare}': {i}"
with :? ArgumentException ->
printfn $"Bad argument: {objectToCompare} (type {objectToCompare.GetType().Name})"
// The example displays the following output:
// Bad argument: TestClass (type TestClass)
// Comparing 'some text' with 'TestClass': -1
// Bad argument: 123 (type Int32)
// Comparing 'some text' with '123': 1
// Comparing 'some text' with 'some text': 0
// Comparing 'some text' with 'Some Text': -1
Public Class TestClass
End Class
Public Class Example
Public Shared Sub Main()
Dim test As New TestClass()
Dim objectsToCompare() As Object = { test, test.ToString(), 123,
123.ToString(), "some text",
"Some Text" }
Dim s As String = "some text"
For Each objectToCompare In objectsToCompare
Try
Dim i As Integer = s.CompareTo(objectToCompare)
Console.WriteLine("Comparing '{0}' with '{1}': {2}",
s, objectToCompare, i)
Catch e As ArgumentException
Console.WriteLine("Bad argument: {0} (type {1})",
objectToCompare,
objectToCompare.GetType().Name)
End Try
Next
End Sub
End Class
' The example displays the following output:
' Bad argument: TestClass (type TestClass)
' Comparing 'some text' with 'TestClass': -1
' Bad argument: 123 (type Int32)
' Comparing 'some text' with '123': 1
' Comparing 'some text' with 'some text': 0
' Comparing 'some text' with 'Some Text': -1
注解
value
必须是 对象 String 。
此方法使用当前区域性执行单词 (区分大小写和区分区域性) 比较。 有关单词、字符串和序号排序的详细信息,请参阅 System.Globalization.CompareOptions。
有关此方法的行为的详细信息,请参阅 方法的 String.Compare(String, String) “备注”部分。