Converts 2D string tab to Dictionary. String[][] contains two string[], first with keys and second with values. - CSharp System

CSharp examples for System:String Convert

Description

Converts 2D string tab to Dictionary. String[][] contains two string[], first with keys and second with values.

Demo Code


using System.Xml.Serialization;
using System.Collections;
using System.Globalization;
using System.Collections.Specialized;
using System.Text;
using System.Collections.Generic;
using System;/* w w w  .j  a v  a  2s  . c o m*/

public class Main{
        /// <summary>
		/// Converts 2D string tab to Dictionary.
		/// String[][] contains two string[], first with keys and second with values.
		/// </summary>
		/// <param name="tab">The tab.</param>
		/// <returns>Dictionary</returns>
		public static Dictionary<string, string> Convert2DStringArrToDictionaryStringString(string[][] tab)
		{
			if (tab == null || tab.Length != 2)
				return null;
			if (tab[0] == null || tab[1] == null
				|| tab[0].Length != tab[1].Length)
				return null;

			Dictionary<string, string> res = new Dictionary<string, string>();
			for (int i = 0; i < tab[0].Length; i++)
			{
				res.Add(tab[0][i], tab[1][i]);
			}
			return res;
		}
}

Related Tutorials