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 實例與物件,所以方法會擲回 TestClass
ArgumentException 。
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
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 物件。
這個方法會使用目前的文化特性,執行文字 (區分大小寫且區分文化特性的) 比較。 如需 word、字串和序數排序的詳細資訊,請參閱 System.Globalization.CompareOptions 。
如需此方法行為的詳細資訊,請參閱方法的「備註」一節 String.Compare(String, String) 。