Simplyprofound.com
Flash, Flex, AIR, ASP.NET, PHP, AJAX, whatever it takes...

Using C# Arrays

March 11, 2008 16:55 by dave

I use arrays tirelessly in actionscripting, and I always found it odd that I don't use them much in C#. At least I don't use them in the same way.

I recently had the opportunity to remember some basic usage and thought I'd pass it along. I have a small list of strings and want to store them in an array so I can quickly search for the existence of a value.

To create the single dimension array looks like this.

string[] names = new string[] {"Toby","Lucy","Maggie","Rocco","Hank","Mallard"};

If you wish to initialize the size of the array it's just  

string[6] names = new string[6] {"Toby","Lucy","Maggie","Rocco","Hank","Mallard"};

Then you can search the sorted array for the presence of a value by using Array.BinarySearch(). This returns the position of the value in the array (and not necessarily the first occurance), or a negative number if no matching item is found. 

string[] names = new string[] {"Toby","Lucy","Maggie","Rocco","Hank","Mallard"};
Array.Sort(names);
bool exists = (Array.BinarySearch(names,"Toby") >= 0); 

Just google C# arrays and you'll find a plethora of resources on how to use Arrays in your C# projects. C-sharpcorner.com has always been good to me.


Tags: , ,
Categories: ASP.NET
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]