Good day all
So I know that this is a fairly widely discussed issue, but I can\'t seem to find a definitive answer. I know that you can do what I am asking for using a L
Your question is a little off the mark. In particular, you say "that the element needs to be added into the first empty slot in an array, lie (sic) a Java push function would do."
The verb "Push" is not something that is used with an Array in any language that I know of except JavaScript. I suspect that it's only in JavaScript because it could be there (since JavaScript is a completely dynamic language). I'm pretty sure it wasn't designed in intentionally.
A JavaScript-style Push operation in C# could be written in this somewhat inefficient manner:
int [] myArray = new int [] {1, 2, 3, 4};
var tempList = myArray.ToList();
tempList.Add(5);
myArray = tempList.ToArray(); //equiv: myArray.Push(5);
"Push" is used in some types of containers, particularly Stacks, Queues and Deques (which get two pushes - one from the front, one from the back). I urge you not to include Push as a verb in your explanation of arrays. It adds nothing to a CS student's vocabulary.
In C#, as in most traditional procedural languages, an array is a collection of elements of a single type, contained in a fixed length contiguous block of memory. When you allocate an array, the space for every array element is allocated (and, in C# those elements are initialized to the default value of the type, null for reference types).
In C#, arrays of reference types are filled with object references; arrays of value types are filled with instances of that value type. As a result, an array of 4 strings uses the same memory as an array of 4 instance of your application class (since they are both reference types). But, an array of 4 DateTime instances is significantly longer that of an array of 4 short integers.
In C#, an instance of an array is an instance of System.Array, a reference type. Arrays have a few properties and methods (like the Length property). Otherwise, there isn't much you can do with an array: you can read (or write) from (or to) individual elements using an array index. Arrays of type T also implement IEnumerable
, so you can iterate through the elements of an array.
Arrays are mutable (the values in an array can be written to), but they have a fixed length - they can't be extended or shortened. They are ordered, and they can't be re-arranged (except by swizzling the values manually).
C# arrays are covariant. If you were to ask the C# language designers, this would be the feature they regret the most. It's one of the few ways you can break C# type safety. Consider this code (assuming that Cat and Dog classes inherit from Animal):
Cat[] myCats = new Cat[]{myCat, yourCat, theirCat};
Animal[] animals = (Animal[]) myCats; //legal but dangerous
animals[1] = new Dog(); //heading off the cliff
myCats[1].Speak(); //Woof!
That "feature" is the result of the lack of generic types and explicit covariance/contravariance in the initial version of the .NET Framework and the urge to copy a Java "feature".
Arrays do show up in many core .NET APIs (for example, System.Reflection). They are there, again, because the initial release did not support generic collections.
In general, an experienced C# programmer will not use many arrays in his applications, preferring to use more capable collections such as List
, Dictionary
, HashSet
and friends. In particular, that programmer will tend to pass collections around using IEnumerable
an interface that all collections implement. The big advantage of using IEnumerable
as parameters and return types (where possible and logical) is that collections accessed via IEnumerable
references are immutable. It's kinda-sorta like using const
correctly in C++.
One thing you might consider adding in to your lectures on arrays - after everyone has mastered the basics - is the new Span
type. Spans may make C# arrays useful.
Finally, LINQ (Language Integrated Query) introduced a lot of functionality to collections (by adding Extension Methods to IEnumerable
). Make sure your student do not have a using System.Linq;
statement up at the top of their code - mixing LINQ in to a beginning student's class on arrays would bewilder him or her.
BTW: what kind of class is it you teach? At what level?