A
A
Anton Neverov2017-05-02 16:51:27
Programming
Anton Neverov, 2017-05-02 16:51:27

How to make a "deep" array with different data types in C#?

Hello!
I just started learning C# and immediately ran into this problem:
In my experience with PHP and I could use "deep" arrays with different formats of keys and their values. Moreover, the value itself could become an array, in which data types were also combined.
It turned out something like this:
2NQvWfrBJns.jpg
I.e. the array contains the ad network ID, and inside this key there are name and utm_source values.
A similar structure is also in JSON.
In C#, I did not find this, only a strict dictionary was found, which has a Key:Value. But I would like something similar to PHP arrays, because it is necessary to store many values, their names and additional parameters.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tom Nolane, 2017-05-02
@TTATPuOT

in your case, (I personally) would use something like this:

Dictionary<string, string[]> wordDictionary = new Dictionary<string, string[]>();
wordDictionary.Add("YANDEX", new string[] {'yandex1','yandex2'});
wordDictionary.Add("GOOGLE", new string[] {'google1','google2'});

or so
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            List<UserInfo> my_list = new List<UserInfo>();
      my_list.Add(new UserInfo { name = "Вася", age = 19, male = true});
      my_list.Add(new UserInfo { name = "Маша", age = 23, male = false});

            Dictionary<string, UserInfo> dic = new Dictionary<string, UserInfo>();
            dic.Add("YAndeX", my_list[0]);
            dic.Add("GooGle", my_list[1]);

            foreach (var temp in dic)
            {
                Console.WriteLine("Key: " + temp.Key + " Value: Age" + temp.Value.age + " Name: " + temp.Value.name + " is male: " + temp.Value.male);
            }
            Console.ReadLine();
        }
    }

    class UserInfo
    {
        public string name { get; set; }
        public int age { get; set; }
        public bool male { get; set; }
    }
}

-------------------------------------------------- ------
I hope it helps you:
1) arrays for dating
2) tuple
3) class Dictionary< TKey, TValue >
using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class UserInfo
    {
        // Метод, реализующий словарь
        public static Dictionary<int, string> MyDic(int i)
        {
            Dictionary<int, string> dic = new Dictionary<int,string>();
            Console.WriteLine("Введите имя сотрудника: \n");
            string s;
            for (int j = 0; j < i; j++)
            {
                Console.Write("Name{0} --> ",j);
                s = Console.ReadLine();
                dic.Add(j, s);
                Console.Clear();
            }
            return dic;
        }
    }

    class Program
    {
        static void Main()
        {
            Console.Write("Сколько сотрудников добавить? ");
            try
            {
                int i = int.Parse(Console.ReadLine());
                Dictionary<int, string> dic = UserInfo.MyDic(i);

                // Получить коллекцию ключей
                ICollection<int> keys = dic.Keys;

                Console.WriteLine("База данных содержит: ");
                foreach (int j in keys)
                    Console.WriteLine("ID -> {0}  Name -> {1}",j,dic[j]);
            }
            catch (FormatException)
            {
                Console.WriteLine("Неверный ввод");
            }

            Console.ReadLine();
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question