This documentation is archived and is not being maintained.

Attribute.GetHashCode Method

Returns the hash code for this instance.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public override function GetHashCode() : int

Return Value

Type: System.Int32
A 32-bit signed integer hash code.

The following code example illustrates the use of GetHashCode in the context of Attribute.

import System;
import System.Reflection;
import System.Collections;

package HashCodeJS {
	// A custom attribute to allow 2 authors per method.
	public AttributeUsage(AttributeTargets.Method) class AuthorsAttribute extends Attribute {
		public function AuthorsAttribute(name1 : String, name2 : String) {
			authorName1 = name1;
			authorName2 = name2;
		}

		protected var authorName1 : String;
		protected var authorName2 : String;
	
		public function get AuthorName1() : String {
			return authorName1; 
                }

                public function set AuthorName1(AuthorName1 : String) {
			authorName1 = AuthorName1; 
                }

		public function get AuthorName2() : String{
			return authorName2; 
                }

                public function set AuthorName2(AuthorName2 : String) {
			authorName2 = AuthorName2; 
		}

		// Use the hash code of the string objects and xor them together.
		public override function GetHashCode() : int {
			return authorName1.GetHashCode() ^ authorName2.GetHashCode();
		}
	}

	// Provide the author names for each method of the class.
	public class TestClass {
		public AuthorsAttribute("Immanuel Kant", "Lao Tzu")
		function Method1()
		{}

		public AuthorsAttribute("Jean-Paul Sartre", "Friedrich Nietzche")
		function Method2()
		{}

		public AuthorsAttribute("Immanuel Kant", "Lao Tzu")
		function Method3()
		{}

		public AuthorsAttribute("Jean-Paul Sartre", "Friedrich Nietzche")
		function Method4()
		{}

		public AuthorsAttribute("Immanuel Kant", "Friedrich Nietzche")
		function Method5()
		{}
	}

	class DemoClass {
		static function Main() {
			// Get the class type to access its metadata.
                        var testclass : TestClass = new TestClass();
			var clsType : Type = testclass.GetType();

			// Use a hash table to store the hash codes and methods.
			var hTable : Hashtable = new Hashtable();

			// Iterate through all the methods of the class.
                        var mInfo : MethodInfo[] = clsType.GetMethods()
			for (var i : int = 0; i < mInfo.length; i++) {
				// Get the Authors attribute for the method if it exists.
				var attr : Attribute = Attribute.GetCustomAttribute(mInfo[i], AuthorsAttribute);
				if (attr != null) {
					// Get the unique hash code for these authors.
					var hCode : int = attr.GetHashCode();

					// See if it's already been added to the hash table.
					if (hTable.Contains(hCode)) {
						// If so, retrieve the array import the Item syntax.
						var al : ArrayList = ArrayList(hTable[hCode]);
						// Add the method to the array list.
						al.Add(mInfo[i].Name);
						// Put the edited array list back into the table.
						hTable[hCode] = al;
					}

					// This is the first occurrence of this hash code.
					else {
						// Create a new array list.
						var al2 : ArrayList = new ArrayList();
						// Add the method name as the first entry.
						al2.Add(mInfo[i].Name);
						// Add the new hash code and array list to the table.
						hTable.Add(hCode, al2);
					}
				}
			}

			// Get the hash table's enumerator and iterate through the table.
			var dictEnum : IDictionaryEnumerator = hTable.GetEnumerator();
			while (dictEnum.MoveNext()) {
				// Get the array list for this iteration.
				var al3 : ArrayList = ArrayList(dictEnum.Value);
				// Access each element of the array.
				for (var j : int = 0; j < al3.Count; j++) {
					// For the first element, retrieve the author names.
					if (j == 0) {
						// Get the metadata for the method.
						var mInfo2 : MethodInfo = clsType.GetMethod(String(al3[j]));
						// Retrieve the Authors attribute again.
						var attr2 : Attribute = 
                                                      Attribute.GetCustomAttribute(mInfo2, 
                                                                                   AuthorsAttribute);
						// Display the authors names.
						if (attr2 != null)
							Console.WriteLine("Authors: {0}, {1}", 
								AuthorsAttribute(attr2).AuthorName1, 
                                                                AuthorsAttribute(attr2).AuthorName2);
						else
							Console.WriteLine("The authors could not be retrieved.");
					}
					// Display each method authored by this team.
					Console.WriteLine(al3[j]);
				}
				Console.WriteLine("");
			}
		}
	}
}

/*
 *  Output:
 * Authors: Jean-Paul Sartre, Friedrich Nietzche
 * Method4
 * Method2
 * 
 * Authors: Immanuel Kant, Lao Tzu
 * Method3
 * Method1
 *
 * Authors: Immanuel Kant, Friedrich Nietzche
 * Method5
 */

HashCodeJS.DemoClass.Main();

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: