• 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
1
Question by easytoyog · Jun 03, 2015 at 05:26 AM · c#arraylist

How to make a list of string within list in C#

Hi Everyone,

I am new to Unity and I used to program in python.

I want to make a list of string within a list for example:

mylist = [['tom','1'],['john','2'],['jane','3']] so mylist[0] would be ['tom','1'] and mylist[0][0] would be 'tom'.

Thanks!

Comment
Add comment · Show 1
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 gjf · Jun 02, 2015 at 10:52 PM 0
Share

that's more of a generic c# question than unity-specific but look for 2 dimensional string arrays or create a class containing 2 strings and then a list containing those.

it all depends on the use case and that's too vague and not well suited to unity answers...

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by jmgek · Jun 03, 2015 at 05:40 AM

There are a few things diffrent in C# and python but I think you mean something else when you ask for a list. First to handle "Lists" you need to use system generic, like include in python (if I remember correctly) Yes you can have a list of lists and that would look like this,

 List<List<string>> crazyListOfStrings = new List<List<string>>(); 

But I would not suggest that.

This is how you create a list in untiy:

 using System;
 using System.Collections.Generic;
 
 List<string> names = new List<string>();
 //If you wanted to append to the top of the list
 names.add("Tom");
 names.add("Tom2");
 //Now the list contains
 //names[0] == "Tom"
 //names[1] == "Tom2"

But what I think you are looking for is a dictionary and that is almost the exact same way:

 using System;
 using System.Collections.Generic;
 
 class Program
 {
     static void Main()
     {
     Dictionary<string, int> dictionary =
         new Dictionary<string, int>();
 
     dictionary.Add("Tom", 2);
     dictionary.Add("Toms dad", 1);
     dictionary.Add("Toms mom", 0);
     dictionary.Add("Toms kinky boyfriend", -1);
     }
 }
Comment
Add comment · Show 2 · 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 easytoyog · Jun 04, 2015 at 02:05 AM 0
Share

Awesome! This works great, thanks a lot!

avatar image Vivien_Lynn · Sep 14, 2019 at 04:11 PM 0
Share

The "add" in the first example need to be written with a capital "A", like so: names.Add("$$anonymous$$"); names.Add("$$anonymous$$2");

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Detecting OnMouseDown from colliders in an array 1 Answer

How to make a or array of GUI.Button's? 1 Answer

Pick between two floats 2 Answers

properly initializing my arrays (C#) 2 Answers

Value will not return from ArrayList, C# 1 Answer


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