• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Hashtable Class in C#

January 21, 2022 C#

In this post, you will learn how to work with the Hashtable class so that you can use that same knowledge and thus evolve.

  • this class (belonging to the System.Collections library) represents a collection of associations between value pairs;
  • the first element of the pair is called the key (identifier) and the other is called the content;
  • the collection of value pair associations are organized according to the hash code of the key.
using System;
using System.Collections;

namespace Base {
  class HashtableClass {
    private Hashtable people = new Hashtable() {
      { "Nelson Silva", 25 },
      { "Larissa Fernandes", 37 }
    };

    public void Run() {
      people.Add("Pedro Henrique", 52);
      people.Add("Raquel Soares", 68);

      people["Pedro Henrique"] = 100;
      people.Remove("Larissa Fernandes");
      people.Clear();

      Console.WriteLine($"Number of people: {people.Count}");

      foreach (DictionaryEntry person in people) {
        Console.WriteLine($"Name: {person.Key}");
        Console.WriteLine($"Age: {person.Value}");
      }
    }
  }
}

/*
  Number of people: 3
  Name: Raquel Soares
  Age: 68
  Name: Nelson Silva
  Age: 25
  Name: Pedro Henrique
  Age: 100
*/

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved