C# Dictionaries?

I’m trying to make a dictionary when I can easily find out values.

In Lua you can do:

local Blocks = {}
Blocks[×][y][z] = “SomeValue”

What is the C# equivalent to that? I’m new at C# and Unity (after coming from ROBLOX)

2 Likes
Dictionary<keytype, objectType> dictionaryName;

Dictionary <string, string> fruits = new Dictionary<string, string>();

fruits.Add("apple","macintosh");
5 Likes

Are you able to to stuff like this with that code?

fruit[“apple”] = “Ripe”;

Debug.Log(fruit[“apple”]); // Logs “Ripe”

Yes.

Would anyone mind giving me a working example of dictionaries in C#?

http://lmgtfy.com/?q=c%23+dictionary+tutorial#

2 Likes

Your problem might be that you’re including System.Collections.Generic at the top (with a using statement).

Hang on a sec, I’ll stick something up here.

        Dictionary <string, string> fruits = new Dictionary<string, string>();
        fruits.Add("apple","macintosh");
        Debug.Log( fruits["apple"]);
1 Like

I’m getting these errors:
cs(9,17): error CS1519: Unexpected symbol (' in class, struct, or interface member declaration cs(11,19): error CS0178: Invalid rank specifier: expected ,’ or `]’

With this code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Inventory : MonoBehaviour {

Dictionary<string, string> Test = new Dictionary<string, string>();

Test.Add(“Test1”,“Test2”);

Debug.Log(Test[“Test1”]);

}

http://lmgtfy.com/?q=c%23+tutorial#

Copy pasting random snippets with no understanding of them isn’t going to get you very far…

But just because I’m a nice guy, the last two lines of your code need to go into a method.

you need to do some basic scripting tutorials.

1 Like

You can learn about using MonoBehaviour here:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;// this namespace is required to import dictionaries

public class dictionaryExample : MonoBehaviour {
	public Dictionary<string, bool> myDictionary = new Dictionary<string, bool>();// declair key type and value type : <string, bool>
	public Dictionary<string, string> myDictionary2 = new Dictionary<string, string>();

	// Use this for initialization
	void Start () {
		if (myDictionary.Count < 10) {// gets count on dictionary
			string c = myDictionary.Count.ToString ();
			myDictionary.Add (c, false); // adds to dictionary
			myDictionary2.Add (c, c);
		}
		foreach (KeyValuePair<string, bool> i in myDictionary) { //you can use this  to return keys and values (key value pair)
			Debug.Log (i.Key);// i.Key will rwtun the key for that KVP
			Debug.Log(i.Value);// i.Value returns value fot the KVP
		}
		foreach (var i in myDictionary2) {// var can me used as a shortcut for many class types (in this case KeyValuePair <string, string>)
			Debug.Log (i.Key);
			Debug.Log(i.Value);
		}
		Debug.Log (myDictionary ["0"]);// this overload : myDictionary["0"]   will return the value associated to the Key : "0"
		if (myDictionary.ContainsKey ("9")) {
			myDictionary.Remove ("9"); // will remove a KVP
			Debug.Log ("Has Removed");
		}
		myDictionary2.Clear ();// will clear the dictionary
	}
}
8 Likes

Do you have a specific application for your dictionaries or just a general rundown of what they are? The C# page on .NET is pretty good at showing them off.

It’s a spam bot.

1 Like

I had a hunch given the links, maybe the bot wanted to learn about dictionaries? :stuck_out_tongue: Cheers

1 Like

Whoa, Kurt’s posts are starting to be quoted by AI bots directly in the same thread now.

1 Like