Adding items to Dictionary without using Add method in C# - c#

I have items and I want to add them to Dictionary Without using Add method (because it consumes number of lines). Is there any way to add items to Dictionary like
new List<string>() { "P","J","K","L","M" };
or like AddRange Method in List. Any help will be highly appericiated.

Referenced from here
Dictionary<int, StudentName> students = new Dictionary<int, StudentName>()
{
{ 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}},
{ 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}},
{ 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}}
};

You can easily create an extension method that does an AddRange for your dictionary
namespace System.Collections.Generic
{
public static class DicExt
{
public static void AddRange<K, V>(this Dictionary<K, V> dic, IEnumerable<K> keys, V v)
{
foreach (var k in keys)
dic[k] = v;
}
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var list = new List<string>() { "P", "J", "K", "L", "M" };
var dic = new Dictionary<string, bool>();
dic.AddRange(list, true);
Console.Read();
}
}
}

it's as easy as
var dictionary = new Dictionary<int, string>() {{1, "firstString"},{2,"secondString"}};

Related

how to call a dictionary with a string

I have 4 dictionary, each contain a button's name and the button's value.
I Have a List that contain the name of each dictionary
private Dictionary<string, int> TableArray = new Dictionary<string, int>() { { "ButtonRMT35", 35 }, { "ButtonRMT17", 17 }, { "ButtonRMT11", 11 }, { "ButtonRMT8", 8 }, { "ButtonRMT5", 5 } };
private Dictionary<string, int> ParArray = new Dictionary<string, int>() { { "ButtonRMP20", 20 }, { "ButtonRMP15", 15 }, { "ButtonRMP10", 10 }, { "ButtonRMP5", 5 } };
private Dictionary<string, int> MaxChipsRPArray = new Dictionary<string, int>() { { "ButtonRPC20", 20 }, { "ButtonRPC15", 15 }, { "ButtonRPC10", 10 }, { "ButtonRPC5", 5 } };
private Dictionary<string, int> QuestionSerieRPArray = new Dictionary<string, int>() { { "ButtonRPQ20", 20 }, { "ButtonRPQ15", 15 }, { "ButtonRPQ10", 10 }, { "ButtonRPQ5", 5 } };
public List<string> DictionaryList = new List<string>() { "TableArray", "ParArray", "MaxChipsRPArray", "QuestionSerieRPArray" };
I would like to do something like that
foreach (var dictionnary in DictionaryList)
{
foreach (var buttonName in dictionnary.Keys)
{
DoSomething();
}
}
Is there a way to do that?
The List<string> (DictionaryList) contains strings. Those strings are not variable identifiers (variable identifiers are not the same as C#/.NET strings; variable identifiers are lexical tokens in the C# language, being parsed by the C# compiler during the build of your program), and thus cannot be used to refer to some variable.[1]
Rather than maintaining strings in your DictionaryList, let it maintain the dictionaries itself:
private Dictionary<string, int> TableArray = ...
private Dictionary<string, int> ParArray = ...
private Dictionary<string, int> MaxChipsRPArray = ...
private Dictionary<string, int> QuestionSerieRPArray = ...
public List<Dictionary<string, int>> DictionaryList = new List<Dictionary<string, int>>()
{
TableArray, ParArray, MaxChipsRPArray, QuestionSerieRPArray
};
If you need to access the dictionaries by some name provided as a string (regardless whether that name would correlate with the variable/field names) you can turn the list into a dictionary of dictionaries (mapping some name to each of your dictionaries) instead:
private Dictionary<string, int> TableArray = ...
private Dictionary<string, int> ParArray = ...
private Dictionary<string, int> MaxChipsRPArray = ...
private Dictionary<string, int> QuestionSerieRPArray = ...
public Dictionary<string, <Dictionary<string, int>>> Dictionaries =
new Dictionary<string, <Dictionary<string, int>>>()
{
["TableArray"] = TableArray,
["MaxChipsRPArray"] = MaxChipsRPArray,
["QuestionSerieRPArray"] = QuestionSerieRPArray
};
...
foreach (var dictionary in Dictionaries.Values)
{
foreach (var buttonName in dictionary.Keys)
{
DoSomething();
}
}
You could then access an individual dictionary by name through the public Dictionaries field like this, for example:
var someDictionaryIWant = Dictionaries["MaxChipsRPArray"];
foreach (var buttonName in someDictionaryIWant.Keys)
{
DoSomething();
}
[1] I just told a grey lie here. For fields and properties, it would be possible to access fields/properties by their field/property name given as a string through a mechanism called "reflection". But reflection is cumbersome (likely even complicated for inexperienced programmers), slow, does not play well with trimming or compiling into native code, is normally not applicable to local variables declared inside methods, etc... But i think it's doubtful that you are looking for some dirty hack-ish way to solve your problem when there are cleaner and more straightforward solutions to your problem.
You are on a right track, however it would be hard to achieve by using variable names.
Use nested list, like this: List<Dictionary<string, int>> dictionaryList
Then add your dictionaries to the list, and iterate over them in the for each loop like you initially wanted to.

Multi string array to multi dictionary in C#?

I got stuck to change the following array to a dictionary.
public static string[][,] patterns = new string[][,]
{
new string[,] {
{ "1,2,3" },
{ "3,2,5" },
},
new string[,] {
{ "4,4,3" },
{ "7,1,2" },
},
};
This is what I have:
public Dictionary<string, string[]> patterns = new Dictionary<string, string[]>();
I can't fill the array with predefined values.
I want to change to a dictionary, because it has a key.
Can I also change the above array to a key and values format?
I want something like this: { "keyNameExample1", "1,2,3", "4,5,6", "etc"}. I want do something like this: patterns["keyNameExample", 1 (integer array pack)]; or patterns["keyNameExample", 2]; (to get the second arrays)
{ "keyNameExample1", "1,2,3", "4,5,6", "etc"} { "keyNameExample2", "5,7,8", "1,1,1", "etc"} and get it like this: patterns["keyNameExample1", 2]; or patterns["keyNameExample2", 1];
can make it even shorter like:
public static Dictionary<string, string[]> demo = new Dictionary<string, string[]>
{
{ "abc", new[]{"1","2"}},
{ "def", new[]{"3","4"}},
};
and with C# 9 you can even do:
public static Dictionary<string, string[]> demo = new()
{
{ "abc", new[]{"1","2"}},
{ "def", new[]{"3","4"}},
};
You can just make it a dictionary with a list, and I guess that covers your requirements (index access, variable number of integers for the index element). Here is an example (for the value list I am not sure, whether you really want a string or ints, in case just change the type):
// define dictionary
IDictionary<string, IList<int>> dict = new Dictionary<string, IList<int>>();
// assign values
dict["abc"] = new List<int> { 2, 4, 8 };
dict["def"] = new List<int> { 10, 12, 14 };
// get value
int dictDef2 = dict["def"][1];
Finally, I got it.
public static Dictionary<string, string[]> demo = new Dictionary<string, string[]>
{
{ "abc",
new string[]
{
"1",
"2"
}
},
{ "def",
new string[]
{
"3",
"4"
}
},
};
Contains check:
if (demo.ContainsKey("abc"))
{
}
Get the value(s):
demo["abc"][0]
Thanks for any help.
If you want to change existing array, you can try Linq:
using System.Linq;
...
public static string[][,] patterns = new string[][,] {...}
...
public Dictionary<string, string[]> patternsDict = patterns
.Select((value, index) => (
key : $"keyNameExample{index}",
value : value.OfType<string>().ToArray()))
.ToDictionary(pair => pair.key, pair => pair.value);
Note, that we have to convert (flatten) 2d array into ordinal one
If you want just to declare dictionary and fill it:
public Dictionary<string, string[]> patterns = new Dictionary<string, string[]>() {
{"keyExample1", new string[] { "1,2,3", "3,2,5" }},
{"keyExample1", new string[] { "4,4,3", "7,1,2" }},
};
public string[][,] patterns = new string[][,] { new string[,] { { "1,2,3" }, { "3,2,5" } }, new string[,] { { "4,4,3" }, { "7,1,2" } } };
Dictionary<string, string[,]> patternsDictionary = new Dictionary<string, string[,]>();
for (int i = 0; i < patterns.Length; i++)
{
patternsDictionary.Add(i.ToString(), patterns[i]);
}
Console.WriteLine(patternsDictionary["0"][0,1]); // Returns 2 - from { "1,2,3" }

Dictionary.ContainsKey(entry) returns false

I have a string List that contains some numbers .
I have some string in mybuylist that matches in Dictionary list. But this if condition always returns False. Ok mybuylist like [34,45,58] and mcollection(key,value) like this {[565,5]},{[34,1]},{[78,9]}....
public static Dictionary<string, int> mcollection = new Dictionary<string, int>();
public static List<string> mybuylist = new List<string>();
foreach (string entry in mybuylist) {
if (mcollection.ContainsKey(entry))
{
//dosomething
}
}
Hope someone help me about this
It might be the the problem of case-sensitive comparison or Keys are not matching. All Dictionaries are case-sensitive by default. A and a are different. Verify whether the values in mybuylist and mcollection are same or not.
Declare mcollection like below.
public static Dictionary<string, int> mcollection = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
This will ignorecase and A & a are same.
See this.
EDIT 1:
class Program
{
public static Dictionary<string, int> mcollection = new Dictionary<string, int>() { { "565", 5 }, { "34", 1 }, { "78", 9 } };
public static List<string> mybuylist = new List<string>() { "34", "45", "58" };
static void Main(string[] args)
{
foreach (string entry in mybuylist)
{
if (mcollection.ContainsKey(entry))
{
Console.WriteLine(entry);
//dosomething
}
}
}
}
O/P: 34
EDIT 2: Use below code to remove the spaces
mybuylist = mybuylist.ConvertAll(s => s.Trim());

c# syntax shortcut to skip an object name when refering to it multiple times in a row

Was there any shortcut in c# that would allow to simplify this:
List<string> exampleList = new List<string>();
exampleList.Add("Is");
exampleList.Add("it");
exampleList.Add("possible");
and into something like this:
var exampleList = new List<string>();
exampleList {
.Add("is");
.Add("it");
.Add("possible");
}
I know it's possible to assign properties during declaration like this:
var myObject = new MyObject{
Id = "Useful",
Name = "Shortcut"
};
It would be interesting to know if there are other useful shortcuts like that, but I can't find any.
var exampleList = new List<string>() {
"Yes", "kinda", "with", "collection", "initializers" };
Note that you can also do this for multi-parameter Add methods, like dictionaries:
var lookup = new Dictionary<string, int> {
{"abc", 124}, {"def",456}
};
You could write an extension method:
public static class ListExt
{
public static List<T> Append<T>(this List<T> self, params T[] items)
{
if (items != null && self != null)
self.AddRange(items);
return self;
}
}
And then use it like this:
List<string> list = new List<string>();
list.Append("Or", "you", "can", "use", "an", "extension", "method");
list.Append("Or").Append("like").Append("this");

How do I create a List of Dictionaries in .NET?

I am trying to create a List of Dictionary<string,int> items. I am not sure how to add items in list and how to get back the values while traversing the list. I want to use it in C#, like so:
public List<Dictionary<string,int>> MyList= new List<Dictionary<string,int>>();
A lot has changed in 5 years... You can now do the following:
ListDictionary list = new ListDictionary();
list.Add("Hello", "Test1");
list.Add("Hello", "Test2");
list.Add("Hello", "Test3");
Enjoy!
I think this is what you are looking for?
{
MyList.Add(new Dictionary<string,int>()); // "Dictionary 1"
MyList.Add(new Dictionary<string,int>()); // "Dictionary 2"
MyList[0].Add("Dictionary 1", 1);
MyList[0].Add("Dictionary 1", 2);
MyList[1].Add("Dictionary 2", 3);
MyList[1].Add("Dictionary 2", 4);
foreach (var dictionary in MyList)
foreach (var keyValue in dictionary)
Console.WriteLine(string.Format("{0} {1}", keyValue.Key, keyValue.Value));
}
I think you have to know in which of the dictinaries you have to add your new value. So The List is the problem. You can't identify the dictionary inside.
My solution for this would be a dictionary collection class.
It could look like this:
public class DictionaryCollection<TType> : Dictionary<string,Dictionary<string,TType>> {
public void Add(string dictionaryKey,string key, TType value) {
if(!ContainsKey(dictionaryKey))
Add(dictionaryKey,new Dictionary<string, TType>());
this[dictionaryKey].Add(key,value);
}
public TType Get(string dictionaryKey,string key) {
return this[dictionaryKey][key];
}
}
then you can use it like this:
var dictionaryCollection = new DictionaryCollection<int>
{
{"dic1", "Key1", 1},
{"dic1", "Key2", 2},
{"dic1", "Key3", 3},
{"dic2", "Key1", 1}
};
// Try KeyValuePair Please.. Worked for me
private List<KeyValuePair<string, int>> return_list_of_dictionary()
{
List<KeyValuePair<string, int>> _list = new List<KeyValuePair<string, int>>();
Dictionary<string, int> _dictonary = new Dictionary<string, int>()
{
{"Key1",1},
{"Key2",2},
{"Key3",3},
};
foreach (KeyValuePair<string, int> i in _dictonary)
{
_list.Add(i);
}
return _list;
}

Categories

Resources