Coder Social home page Coder Social logo

mydictionary's Introduction

MyDictionary

C#6.0 .NET FRAMEWORK 4.6

The project illustrate how You can create Your own Dictionary.


Purpose

In way, if You want to create Your Own dictionary with some functionality thes example will be useful.


MyDictionary class implementation

public class MyDict<T1, T2> : IEnumerable
      where T2 : class
{
    //Fields
    private List<T1> List1 = new List<T1>();
    private List<T2> List2 = new List<T2>();

    //Ctors
    public MyDict()
    {


    }

    public MyDict(T1 value) 
    {

    }

    public MyDict(MyDict<T1, T2> my)
    {
      List1 = my.List1;
      List2 = my.List2;
    }

    public MyDict(T1 value1, T2 value2)
    {
      List1.Add(value1);
      List2.Add(value2);
    }

    //Properties

    public int Count
    {
      get
      {
        return List1.Count;
      }
    }

    //Methods
    public void Add(T1 value1, T2 value2)
    {
      List1.Add(value1);
      List2.Add(value2);
    }

    public void cler()
    {
      List1.Clear();
      List2.Clear();
    }

    public bool ContainsKey(T1 value)
    {
      for (int i = 0; i < List1.Count; i++)
      {
        if (List1.Contains(value))
          return true;
      }
      return false;

    }

    public bool ContainsValue(T2 value)
    {
      foreach (T2 t in List2)
        if (t == value)
        {
          Console.WriteLine(t);
          return true;
        }

      return false;

    }

    public void ShowDict()
    {

      for (int i = 0; i < List1.Count; i++)
      {
        Console.WriteLine($"The {i + 1} pair is: {List1[i]}-{List2[i]}");
      }

    }

    public IEnumerator GetEnumerator()
    {
      return List1.GetEnumerator();
    }   
    }
}

Description of MyDictionary class

MyDict class is the generic class and implements IEnumerable interface. The class has 4 constructors of any object initialization, two Fields of List, one property which return the count of values in Dictionary and several methods:

  • void Add(T1 value1, T2 value2)
  • void clear()
  • bool ContainsKey(T1 value)
  • bool ContainsValue(T2 value)
  • void ShowDict()
  • IEnumerator GetEnumerator()

We can add any functionality for this class.

Program class implementation

// Creating two Dictionaries with constructors
     MyDict<string, string> dict1 = new MyDict<string, string>();
     MyDict<string, string> dict2 = new MyDict<string, string>("FirstName1", "Lastname1");

     // Adding some values
     dict2.Add("FirstName2", "Lastname2");
     dict2.Add("FirstName3", "Lastname3");

     // Showing the content of dictionary
     dict2.ShowDict();

     // Printing the count of dictionary
     Console.WriteLine(dict2.Count);

     // Creating another dictionary by copying in constructor 
     MyDict<string, string> dict3 = new MyDict<string, string>(dict2);
     dict3.ShowDict();
     Console.WriteLine();

     // Checking availability оф values
     Console.WriteLine(dict2.ContainsKey("FirstName1"));

     // Implementing foreach loop for MyDictionary class
     foreach(string str in dict2)
       Console.WriteLine(str);

Here are Illustrated the functionality of MyDictionary class, where were created the objects, were invoked the class methods: add(), showDict and etc.

Result

N|Solid

As You can see it is easy to use.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.