Creating and initializing a class derived from a dictionary

initialize dictionary c#
c# dictionary contains
c# initialize dictionary with array
initialize dictionary python
c# 6 dictionary initializer
c# dictionary get value by index
c# initialize empty dictionary
c# dictionary foreach

Say I have the following class definition:

public class MyClass : Dictionary<int, string>
{
}

How can I create one from an expression something like this (which doesn't work)?

MyClass myClass = myList.ToDictionary(l => l.Key, l => l.value);

implicit operator can't be used in this case because it violates cs0553 user-defined conversion from base class not allowed

You must create a constructor

public class MyClass : Dictionary<int, string>
{
    public MyClass(Dictionary<int, string> dict)
    {
        foreach(var entry in dict)
        {
            this.Add(entry.Key, entry.Value);
        }
    }
}

then

MyClass myClass = new MyClass(myList.ToDictionary(l => l.Key, l => l.value));

or create a static helper to create an instance from Dictionary

How to initialize a dictionary with a collection initializer, Microsoft Build May 19-20, 2020 | Digital event One way to initialize a Dictionary<TKey,TValue>, or any collection whose Add method takes The second uses a public read / write indexer method of the Dictionary class: C# A Dictionary<TKey,TValue> contains a collection of key/value pairs. Its Add method takes two parameters, one for the key and one for the value. One way to initialize a Dictionary<TKey,TValue>, or any collection whose Add method takes multiple parameters,

Enumerable.ToDictionary returns a Dictionary object, but you want to assign to a MyClass. Just because MyClass is a derived type, does not allow any automatic conversion, the same as MyClass cls = new Dictionary<int, string>() is not allowed.

If you want a similar syntax, you could create your own extension methods, for example assuming MyClass has a constructor accepting the dictionary:

public static class EnumerableExtensions
{
    public static MyClass ToMyClass<TSource>(this Enumerable enumerable, Func<TSource,int> keySelector, Func<TSource,string> elementSelector)
    {
        return new Myclass(enumerable.ToDictionary(keySelector, elementSelector));
    }
}

...
MyClass myClass = myList.ToMyClass(l => l.Key, l => l.value);

This also works if MyClass contains a Dictionary rather than extending it, or if it even converts it to an entirely different format.

Inheritance III, 4) Typically the __init__ in the derived class calls the __init__ method of its base derived from the base-class dict: The dict class implements a standard Python All operators/methods working on dicts make sense working on defaultdicts;  The most sane solution here would be to change new Dictionary to new Dictionary<object, object>, but you're not allowed to do that, so you have to create a Dictionary class. Composition is often better than inheritance, but inheriting from Dictionary<object, object> requires almost no code and quickly gets this hack-job done so it might be more

You can also do it by create and use of simple method. Try this:

public static MyClass ConvertToMyClass(Dictionary<int, string> dictionary)
    {
        MyClass obj = new MyClass();
        foreach (var entry in dictionary)
        {
            obj.Add(entry.Key, entry.Value);
        }
        return obj;
    }

And, use as

MyClass myClass = Program.ConvertToMyClass(myList.ToDictionary(l => l.Key, l => l.value));

C# Dictionary, The following diagram illustrates the generic Dictionary class hierarchy. we have specified types of key and value while declaring a dictionary object. An int is So, use IDictionary<TKey, TValue> type variable to initialize a dictionary object. we have used StudentDictionaryComparer which derives IEqualityComparer to​  When constructing a derived class, the derived class constructor is responsible for determining which base class constructor is called. If no base class constructor is specified, the default base class constructor will be used.

How to create a dictionary in Java, The first step to creating a dictionary in Java is to choose a class that implements a The declaration and initialization of a dictionary follows the syntax below:. Java.util.Dictionary Class in Java util.Dictionary is an abstract class, representing a key-value relation and works similiar to a map. Given a key you can store values and when needed can retrieve the value back using its key.

Programming in Python 3: A Complete Introduction to the Python , __init__() call is used to initialize the SortedDict using the base class dict. Object-Oriented Programming Creating Collection Classes Using Inheritance. Python | Initializing dictionary with empty lists In python one usually comes across situations in which one has to use dictionary for storing the lists. But in those cases, one usually checks for first element and then creates a list corresponding to key when it comes.

2.6 Implementing Classes and Objects, We will focus instead on user-defined classes without multiple inheritance and without We implement an instance using a dispatch dictionary that responds to Finally, classes can create new instances, and they apply their __init__  The object initializer syntax enables you to specify arguments for a constructor or omit the arguments (and parentheses syntax). The following example shows how to use an object initializer with a named type, Cat and how to invoke the parameterless constructor. Note the use of auto-implemented properties in the Cat class.

Comments
  • The real question is why do you inherit a dictionary in the first place?
  • @ZoharPeled Its a DTO created in another assembly I cannot change.