C#.NET Script – Associative Array

Associative array is very useful due to its flexibility and expandability. It can contain any data types and unlimited sub arrays. So far, .NET framework has not provide similar functionality with PHP associative array. Don’t worry, we can still do it in C#.NET by utilizing dictionary. We have to use dictionary to keep the order of array element entry. If we use hashtable, the element order will be messed up. In this article, we will show you how dictionary can support the flexibility and expandability of PHP associative array.

Source Code

Here is the Associative Array class code, you may save it to a single file

    //By    : Xhanch Studio
    //URL   : http://xhanch.com/
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections.ObjectModel;
    namespace AssociativeArray{
        public class AssociativeArray{
            public Dictionary<object, object> Item;
            public AssociativeArray(){
               this.Item = new Dictionary<object, object>();
            }
            public void Add(object name, object value){
                this.Item.Add(name, value);
            }
            public void Add(object name, AssociativeArray value){
                this.Item.Add(name, value.Item);
            }
            public Dictionary<object, object>.KeyCollection GetKeys(){
                return this.Item.Keys;
            }
            public bool InArray(object hook){
                return this.Item.ContainsKey(hook);
            }
            public int Count(){
                return this.Item.Count;
            }
            public static bool IsAssociativeArray(){
                return (this.Item.GetType().ToString().StartsWith("System.Collections.Generic.Dictionary"));
            }
        }
    }

Here is how to use the Associative Array class:

using System;
using AssociativeArray;
namespace Test{
    public class Test{
        static void Main(string[] args){
            AssociativeArray arr= new AssociativeArray();
            arr.Add("elm 1", "value 1");
            arr.Add("elm 2", true);
            arr.Add("elm 3", 1);
            //Add a sub array
            AssociativeArray arrChd = new AssociativeArray();
            arrChd .Add("chd 1", "value 1");
            arrChd .Add("chd 2", true);
            arrChd .Add("chd 3", 1);
            arr.Add("elm 4", arrChd);
            // List all AssociativeArray manually
            foreach (string key in arr.GetKeys()){
                //Check if the current element is an associative array
                if(AssociativeArray.IsAssociativeArray(arr.Item[key])){
                    //Accessing sub array
                    AssociativeArray tmp = new AssociativeArray();
                    tmp.Item = arr.Item[key];
                    foreach (string key1 in tmp.GetKeys()){
                        Console.WriteLine(key1 + ": " + tmp.Item[key1 ]);
                    }
                }else
                    Console.WriteLine(key + ": " + arr.Item[key]);
            }
	    // Single Item
            Console.WriteLine("User: " + arr.Item["Line 1"]);
        }
    }
}

Incoming Search Terms

c# associative array, net associative array, associative array c#, associative array in c sharp, c# associative arrays, associative array in c#, c sharp associative array, associative array in net, associative array c sharp, associative array NET, net associative arrays, c# named array, csharp associative array, Associative Arrays in C#, c# associated array, c net associative array, net named array, c# net associative array, csharp associative arrays, associative arrays in c sharp, c# array associativi, c associative array, associate array in C#, c# named arrays, c# associative array hashtable, named array C#, c# assoc array, associative array csharp, associative arrays c#, associative arrays c sharp, csharp assoc array, net collections generic dictionary<object object>, csharp named array, creating associative array in c#, how to make associative array in c#, csharp php arrays named, csharp for each associative array, convert javascript associative array to c# dictionary, foreach associative array in c#, csharp associative arrayx, c# net php post creating associative array, cast associative array to hashtable c#, c# Script array, C# как уничтожить переменную#, how to use associative array in c sharp, indexed array c# net, net new associative array, net screept c#, net static associative array, plugins for dictionary in c, public class associativearray, raport z array list c#, script sharp net, use of associative array in c#, using associative array in c sharp, net named array<, print associative array c#, net managed object to javascript associative array, named array in csharp, named arrays in c#, named indexed array c#, namedarray C#, net Array string key, net associate array, net c associative array, net class associated arrays, net hashed array, vettori associativo c#, array associativo c#, Associative array list c#, associative array net 4, associative array php c#, associative arraylist, associative arraylist c#, associative arrays c# 2 0, associative arrays c# foreach, associativeArray C#, c net associative array key, associative array in c, array associate in c#, assoc array net, associate array c#, associated array c#, associated array in c#, associated arrays in c#, associateve array csharp, associative array c shapr, associative array c# arraylist, Associative Array class in C#, c net associative arrays, c sharp asscoaitivate array, c sharp foreach associative array, C# create associative array, c# create associete array, c# create assozitve array, c# dictionary data plugins, c# foreach associative array, c# foreach assoziatives array, c# Generic class for associative array, c# get keys of associative array, Xhanch, Xhanch Studio


Posted on 2010-02-15 by Susanto BSc in C#.NET Script, Free Script
Free, Associative Array, Flexible, Script, C#, Associative, Dictionary, C#.NET, Code, Element, Hashtable, Expandable, Array, .net, Xhanch, Xhanch Studio

Tagged as: , , , , , , , , , , , , ,

2 Responses to “C#.NET Script – Associative Array”

  1. aleroot says:

    This method in the class :
    public static bool IsAssociativeArray(){
    return (this.Item.GetType().ToString().StartsWith(“System.Collections.Generic.Dictionary”));
    }

    Give me an error “Impossible use Keyword this in static method” !

    When i try to do this in my program :

    mp.Item = arr.Item[key];

    Give me an error :
    Cast Missing , Impossible convert object in ‘System.Collections.Generic.Dictionary’.

    Why ?
    Have you try to use this class ??

    • This class has been tested for sure since I have used it on some of my projects.
      The error you get does not make sense.
      FYI, this class is built under .NET FX 3.5.
      Perhaps it is caused by incompatible version.

Leave a Reply

You must be logged in to post a comment.