Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / C#

A generic Trictionary class

Rate me:
Please Sign up or sign in to vote.
4.53/5 (19 votes)
11 Mar 2009CPOL4 min read 52.9K   140   20  
This article describes a generic Trictionary class derived from Dictionary that allows two values of different types per key
//-----------------------------------------------------------------------
// <copyright file="Trictionary.cs" company="None">
//     Copyright (c) Nish Sivakumar. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace TrictionaryCollection
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    /// <summary>
    /// Represents a dictionary with two distinct typed values per key
    /// </summary>
    /// <typeparam name="TKey">The type of the dictionary key</typeparam>
    /// <typeparam name="TValue1">The type of the first value</typeparam>
    /// <typeparam name="TValue2">The type of the second value</typeparam>
    [Serializable]
    public class Trictionary<TKey, TValue1, TValue2> : Dictionary<TKey, DualObject<TValue1, TValue2>>
    {
        /// <summary>
        /// Initializes a new instance of the Trictionary class
        /// </summary>
        public Trictionary()
        {
        }

        /// <summary>
        /// Initializes a new instance of the Trictionary class for use with serialization
        /// </summary>
        /// <param name="info">SerializationInfo objects that holds the required information for serialization</param>
        /// <param name="context">StreamingContext structure for serialization</param>
        protected Trictionary(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        /// Gets or sets the values associated with the specified key
        /// </summary>
        /// <param name="key">The key of the values to get or set</param>
        /// <returns>Returns the DualObject associated with this key</returns>
        public new DualObject<TValue1, TValue2> this[TKey key] 
        {
            get
            {
                return base[key];
            }

            set
            {
                if (this.ContainsKey(key))
                {
                    base[key].Set(value);                    
                }
                else
                {
                    base[key] = value;
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions