Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by zakkar · Jan 25, 2012 at 11:25 PM · arraysdeclaration

Arrays declaration c#

Hello everybody

Please i'm new here and i want to know how can i declare an Array i have tested this one but it doesn't work

Array aGrid =new Array();

Can anyone help me?

thx

Comment
Add comment
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

3 Replies

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

Answer by leniency · Jan 26, 2012 at 05:00 AM

There are several ways to declare arrays:

 int[] arr1 = new int[3]; // Create an integer array with 3 elements
 var arr2 = new int[3];   // Same thing, just with implicit declaration

 var arr3 = new int[] { 1, 2, 3 };  // Creates an array with 3 elements and sets values.

Lists, as Syclamoth mentioned are generic collections that will expand dynamically as you add elements. They do not require a size on declaration.

 List<int> list1 = new List<int>();
 var list2 = new List<int>();

 var list3 = new List<int> { 1, 2, 3 };  // Initialize the list with 3 elements.
 list3.Add(4); // Add a new element, list3.Count is now 4.
Comment
Add comment · 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
3

Answer by syclamoth · Jan 25, 2012 at 11:27 PM

In C# you don't use those Arrays. Even in JS you should avoid them- they're more trouble than they're worth.

Instead, use System.Collections.Generic.List-

 using System.Collections.Generic;

 List<TypeYouWantInTheList> aGrid = new List<TypeYouWantInTheList>();

The usage is almost exactly the same- List has many more functions, however. If you need to find out what it can do, look it up!

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 leniency · Jan 26, 2012 at 05:08 AM 1
Share

Arrays are still very useful when you have a fixed size and don't want the ($$anonymous$$mal) overhead of a List<>. Plus with Linq, you still get all the IEnumerable<> extensions regardless of using an array or list. $$anonymous$$ostly its about picking the right tool - whether you want to allow other components unrestricted growth, or a cap.

avatar image syclamoth · Jan 26, 2012 at 05:16 AM 0
Share

I'm more talking about the differences between the JS 'Array()' class and the similarly-used generic List. If you want builtin arrays, just use those, but they're not much different in C# than in JS. The question was specifically about alternatives to the Array class.

avatar image
0

Answer by zakkar · Jan 26, 2012 at 10:02 AM

finally i have made this

   Card[,] aGrid;
     
     for(i=0;i<rows;i++)
             {
                 for(j=0;j<cols;j++)
                 {
                 aGrid[i,j]= new Card();    
                 }
             
             
         }
     and i have declared a class of card which contain a string 
 thx
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Declaring an Array as a combination of multiple other arrays? 1 Answer

Declare and clear an Array 3 Answers

Declaring a type in JavaScript arrays 3 Answers

Dynamic multi-dimensional array in C#? 2 Answers

accessing gameObject script from an array 3 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