C# program to print hash table length

WBOY
Release: 2023-09-14 08:05:07
forward
1030 people have browsed it

打印哈希表长度的 C# 程序

In C#, a Hashtable collection is a collection of elements, where each element is composed of key-value pairs. The key of an element is unique and non-null, while the value of an element can be repeated or even empty. Key-value pairs are organized based on the hash code of the key.

This key is used to access elements in the collection. In C#, a class named Hashtable represents a collection of hash tables. This class provides various constructors to construct/create hash table objects. The Hashtable class also provides various methods and properties that we can use to manipulate hash table collections.

Let's discuss how to find out the length of a hash table collection in C# and then print it to the screen.

How to print the length of the hash table?

In C#, the Hashtable class does not provide properties/functions that return the hashtable size or length, unlike in Java. Therefore, we need to bypass this limitation and count the number of elements in the hashtable. The total number of elements in the hashtable is the length of the hashtable.

The Count attribute provided by the Hashtable class can simplify counting elements in the hash table. The Hashtable.Count property returns the total number of key-value pairs in the hash table.

Let's take a look at the prototype of the Count property.

grammar

public virtual int Count { get; }
Copy after login
describe

Get the number of elements (key-value pairs) contained in the Hashtable.

Namespaces

System.Collections
Copy after login

Property Value

Int32 - The number of key-value pairs in the hashtable collection.

accomplish

count

In the prototype shown above, the Count property returns the number of elements (key-value pairs) present in the hash table. This count is considered the length or size of the hash table.

Now let's move on to some programming examples to determine the length of a hash table.

The Chinese translation of

Example

is:

Example

The first programming example we will discuss is given below.

using System; using System.Collections; class MyClass { public static void Main(){ // Creating a Hashtable Hashtable myNumbers = new Hashtable(); // Adding elements in Hashtable myNumbers.Add("10", "Ten"); myNumbers.Add("20", "Twenty"); myNumbers.Add("30", "Thirty"); myNumbers.Add("40", "Forty"); myNumbers.Add("50", "Fifty"); // To get the number of key-value pairs contained in the Hashtable. Console.WriteLine("Length of the hashtable = {0}",myNumbers.Count); } }
Copy after login

In this program, we have a hash table 'myNumbers' which contains numbers as keys and their corresponding numeric names as values. We then execute a statement that returns the number of elements in the hash table, myNumbers.Count.

Output

This property returns the number of elements in the hash table, as shown below.

Length of the hashtable = 5
Copy after login

Since there are 5 elements in the hash table, the length of the hash table is 5.

Now, comment out the following code.

myNumbers.Add("10", "Ten"); myNumbers.Add("20", "Twenty"); myNumbers.Add("30", "Thirty"); myNumbers.Add("40", "Forty"); myNumbers.Add("50", "Fifty");
Copy after login

Output

Here, we have just created the hash table object and commented out the statement of the "Add" method. This means there are no elements in the hash table now. Please check the following output generated.

Length of the hashtable = 0
Copy after login
Copy after login

So the program correctly generates output indicating that the length of the hash table = 0.

Let’s move on to the second example demonstrating Count property. The Chinese translation of

Example

is:

Example

The following program shows the created hash table object. We haven't added any elements to it yet.

using System; using System.Collections; class MyClass { public static void Main() { // Creating an empty Hashtable Hashtable myTable = new Hashtable(); Console.WriteLine("Length of the Hashtable = {0}", myTable.Count); } }
Copy after login

Output

Once the Hashtable object is created, we use the myTable.Count property to output the length of the hashtable. In this case, the output looks like this.

Length of the hashtable = 0
Copy after login
Copy after login

Since there are no elements, the output is 0.

Now we enter the following code in the above program after creating the hash table object.

myTable.Add("US", "United States"); myTable.Add("IND", "India"); myTable.Add("XX", ""); myTable.Add("UK","United Kingdom");
Copy after login

Here we add four elements to the hash table. Please note the fourth element. This element only has the key, but the provided value is empty.

Output

Next, we execute the program and check the output.

Length of the Hashtable = 4
Copy after login

As can be seen from the output, the length of the hash table is four, which also includes an element with a null value.

in conclusion

In this article, we explain and demonstrate the Hashtable.Count property and how to use it to determine the length of a hash table collection. Since there is no direct way to return the size of the hash table, we use this Count property to determine the size of the hash table.

The above is the detailed content of C# program to print hash table length. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!