Compare c# code example
Example 1: string comparison in c#
SYNTAX:
string.Compare(str1, str2);
RULE:
s1==s2 returns 0
s1>s2 returns 1
s1<s2 returns -1
EXAMPLE:
using System;
public class StringExample{
public static void Main(string[] args){
string s1 = "hello";
string s2 = "hello";
string s3 = "csharp";
Console.WriteLine(string.Compare(s1,s2));
Console.WriteLine(string.Compare(s2,s3));
}
}
Example 2: c# compare type
using System;
using System.Reflection;
class Example
{
public static void Main()
{
Type a = typeof(System.String);
Type b = typeof(System.Int32);
Console.WriteLine("{0} == {1}: {2}", a, b, a.Equals(b));
a = typeof(Example);
b = new Example().GetType();
Console.WriteLine("{0} is equal to {1}: {2}", a, b, a.Equals(b));
b = typeof(Type);
Console.WriteLine("typeof({0}).Equals(typeof({1})): {2}", a, b, a.Equals(b));
}
}
Example 3: c# string.compare
public static int Compare (string? strA, string? strB);
Example 4: c# comparer
public abstract class Comparer<T> : System.Collections.Generic.IComparer<T>, System.Collections.IComparer