Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary with limited number of types

Tags:

c#

First, excuse the rather funny name of my question. I'm no native speaker and it took me 10 minutes to express my thoughts in these few characters.

What I'm trying to do is to create a dictionary in C# that allows the value to be either an int, a string or a bool. What first had come to my mind was using generics, but as far as I know, I can only define one type as possible value-type, not "be one of those". Using object would also be possible, but boxing seems to be quite a performance-killer.

Is there any way to do this?

Here's a sample of what has come to my mind:

Dictionary<string, (string, int, bool)> foo = new Dictionary<string, (string, int, bool)>();
foo.Add("key1", "Hello, World!"); //Correct - Value is a string
foo.Add("key2", 37); //Correct - Value is an int
foo.Add("key3", true); //Correct - Value is a boolean
foo.Add("key4", new Foobar()); //Compiler error - Value is a Foobar

My ultimate goal is to provide a library for other developers. This feature should enable them to define "variables" during runtime and give them a type.

Edit://Firefox' about:config page has something very close to what I want to achieve

like image 345
MechMK1 Avatar asked Mar 28 '13 17:03

MechMK1


People also ask

Can a dictionary have multiple types?

One can only put one type of object into a dictionary. If one wants to put a variety of types of data into the same dictionary, e.g. for configuration information or other common data stores, the superclass of all possible held data types must be used to define the dictionary.

Can dictionary values have different types?

For example, you can use an integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable. Values, on the other hand, can be any type and can be used more than once.

Does dictionary have a limit?

There is no such limit in place regarding dictionary keys.

Can a dictionary hold different types Python?

The keys are immutable. Just like lists, the values of dictionaries can hold heterogeneous data i.e., integers, floats, strings, NaN, Booleans, lists, arrays, and even nested dictionaries. This article will provide you a clear understanding and enable you to work proficiently with Python dictionaries.


1 Answers

Why not create a fresh new class which implements the IDictionary and uses a Dictionary as a private variable.

Then, in the add methods, you can provide your own logic and fail accordingly

Sample code

public class MyDic : IDictionary<object, object>
{
    private Dictionary<object, object> privateDic= new Dictionary<object,object>();


    public void Add(object key, object value)
    {
        if (value.GetType() == typeof(string))
            throw new ArgumentException();
        privateDic.Add(key, value);
    }
    //Rest of the interface follows
}
like image 138
Luis Filipe Avatar answered Oct 14 '22 03:10

Luis Filipe