• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by MediaDave · Feb 07, 2012 at 10:14 PM · c#dictionaryhashtable

C# multiple key/value pairs of various types in a single line of code...?

Is there a way to create an object with multiple key/value pairs of differing types in C# like we can with Hashtables in UnityScript in a single line of code?

UnityScript:

var exampleHashtable:Hashtable = {"exampleString":"string", "exampleInt":1, "exampleBoolean:true};

C#

?????? containingObj = {"exampleString":"string", "exampleInt":1, "exampleBoolean:true};

Comment
Novack

People who like this

1 Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Feb 07, 2012 at 10:39 PM 0
Share

Why would you want to? Type-safety exists for a reason.

avatar image MediaDave · Feb 07, 2012 at 11:03 PM 0
Share

I'm porting over a Unity project from UnityScript. It loads external JSON objects that build themselves out by their encapsulated name/value pairs. A parser goes through each of these objects and builds them based on these properties. The properties aren't structured uniformly. For instance, one obj might have {"text":"TITLE TEXT", "font":"Arial", "x":13, "y":22}, while another is {"texture":"/assets/sometexture.png", "width":200, "height"100, "x":50, "y":50}

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Larry-Dietz · Feb 08, 2012 at 01:01 AM

How about using a Dictionary With that you provide a KeyName (string) and and object of any type.

Something like ...

 Dictionary<string, object> Parameters = new Dictionary<string, object>();
 Parameters.Add("Key1", "A string");
 Parameters.Add("Key2", true);
 Parameters.Add("Key3", 42);

The above should create a dictionary holding a string, a bool and an int.

They can be referenced like ...

 string a = Parameters["Key1"] as string;
 bool? b = Parameters["Key2"] as bool?;
 int? c = Parameters["Key3"] as int?;

I believe this will accomplish what you are wanting.

Hope it helps,
-Larry

Comment
ComeSweetDeath
Major0
Novack
devPhil
Hellium
berdanka

People who like this

4 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Winseven4lyf · Oct 09, 2016 at 04:57 AM 0
Share

I was pulling my hair out trying to make a dictionary that holds anything. This was very helpful.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Serializing data structures the Unity way. Possible? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Combine Children Dictionary in place of Hashtable? 2 Answers

How to call the post function of the class Unity post get check www requests with success callback and error callbacks, 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges