本文整理汇总了C#中System.String.GetHashCode方法的典型用法代码示例。如果您正苦于以下问题:C# String.GetHashCode方法的具体用法?C# String.GetHashCode怎么用?C# String.GetHashCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.GetHashCode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
class GetHashCode
{
public static void Main()
{
DisplayHashCode( "" );
DisplayHashCode( "a" );
DisplayHashCode( "ab" );
DisplayHashCode( "abc" );
DisplayHashCode( "abd" );
DisplayHashCode( "abe" );
DisplayHashCode( "abcdef" );
DisplayHashCode( "abcdeg" );
DisplayHashCode( "abcdeh" );
DisplayHashCode( "abcdei" );
DisplayHashCode( "Abcdeg" );
DisplayHashCode( "Abcdeh" );
DisplayHashCode( "Abcdei" );
}
static void DisplayHashCode( String Operand )
{
int HashCode = Operand.GetHashCode( );
Console.WriteLine("The hash code for \"{0}\" is: 0x{1:X8}, {1}",
Operand, HashCode );
}
}
/*
This example displays output like the following:
The hash code for "" is: 0x2D2816FE, 757602046
The hash code for "a" is: 0xCDCAB7BF, -842352705
The hash code for "ab" is: 0xCDE8B7BF, -840386625
The hash code for "abc" is: 0x2001D81A, 536991770
The hash code for "abd" is: 0xC2A94CB5, -1029092171
The hash code for "abe" is: 0x6550C150, 1699791184
The hash code for "abcdef" is: 0x1762906D, 392335469
The hash code for "abcdeg" is: 0x1763906D, 392401005
The hash code for "abcdeh" is: 0x175C906D, 391942253
The hash code for "abcdei" is: 0x175D906D, 392007789
The hash code for "Abcdeg" is: 0x1763954D, 392402253
The hash code for "Abcdeh" is: 0x175C954D, 391943501
The hash code for "Abcdei" is: 0x175D954D, 392009037
*/
示例2: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
// Show hash code in current domain.
DisplayString display = new DisplayString();
display.ShowStringHashCode();
// Create a new app domain and show string hash code.
AppDomain domain = AppDomain.CreateDomain("NewDomain");
var display2 = (DisplayString) domain.CreateInstanceAndUnwrap(typeof(Example).Assembly.FullName,
"DisplayString");
display2.ShowStringHashCode();
}
}
public class DisplayString : MarshalByRefObject
{
private String s = "This is a string.";
public override bool Equals(Object obj)
{
String s2 = obj as String;
if (s2 == null)
return false;
else
return s == s2;
}
public bool Equals(String str)
{
return s == str;
}
public override int GetHashCode()
{
return s.GetHashCode();
}
public override String ToString()
{
return s;
}
public void ShowStringHashCode()
{
Console.WriteLine("String '{0}' in domain '{1}': {2:X8}",
s, AppDomain.CurrentDomain.FriendlyName,
s.GetHashCode());
}
}