0

I will just list out some arrays and ask how to do them in C#.

$myArray = array();

In PHP I don't have to declare a memory size for the array. What if I don't know big I need my array?

$myArray = array("Name" => "Steve");

How do I do the above in C#?

$myArray = array();
$myArray['Names'][0] = "Steve";
$myArray['Names'][1] = "Jim";

How does the above work in C#"?

6
  • 1
    There is a great deal of documentation and tutorials online for C#. I would consult those resources before asking stackoverflow. Commented Sep 12, 2013 at 0:21
  • 2
    Look into using generic collections, such as List<T> and Dictionary<T>. Commented Sep 12, 2013 at 0:22
  • Side note: please avoid unrelated text like "I'm new here", "searched a lot", "learning", "thank you", "help me" in posts on SO. Commented Sep 12, 2013 at 0:30
  • @AlexeiLevenkov help me Commented Sep 12, 2013 at 0:32
  • You should check out what the standard computer science definition of an array is because the "array" in PHP is very different from that, the array in C# ( T[] ) behaves far more like an array in C. Commented Sep 12, 2013 at 0:33

2 Answers 2

3
$myArray = array("Name" => "Steve");

This is a map. PHP doesn't need you to know that, but C# does. You would implement this as:

var myArray = new Dictionary<String, String>();

For your second case

$myArray['Names'][0] = "Steve";

This is a dictionary where the keys are Strings, but the values are String[]. In C#:

var myArray = new Dictionary<String, List<String>>();
Sign up to request clarification or add additional context in comments.

3 Comments

AMAZING! That helps me so so much. I really need that visual tie in! Great community thanks again!
+1: Note that C# encourages strong typing - Instead of Dictionary<string, string> for something like "person with first/last name and title" consider creating type Person with corresponding fields. Than use List<Person> instead of List<Dictionary<String, String>>.
@AlexeiLevenkov brings up a good point. Moving from PHP (dynamically typed) to C# (statically typed, for the most part) isn't only about learning the syntax, but also a change in your thought process.
2

arrays in PHP are most like maps or C# Dictionary . An array in PHP is actually what is called an associative array . So for your code above the C# equivalent is:

        Dictionary<string, string> items= new Dictionary<string, string>();  
        items.Add("Name", "Steve");  

if you want a key to point to multiple values:

        Dictionary<string, ICollection<string>> items= new  
                           Dictionary<string, ICollection<String>>();    
        ICollection<string> names = new List<string>();  
        names.Add("Steve");     
        items.Add("Name", names);

4 Comments

again, there's a reason the var keyword was introduced in C#.
@HighCore when someone is learning a language it is best to be explicit with what you mean. But yes var is very useful.
agreed, however there are cases (precisely when someone comes from other languages) where the excess of explicitness might be seen by some as "a bloated language syntax"
I got it! No worries there. Thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.