G'even gentlemen
I know it hasn't been too long since my previous thread, but once again I'm a little bit curious about a few things (okay, a # of things, but I tried to omit my dumber questions)
The following list are really rhetorical questions, so perhaps I'm not looking for "answers" per se, but just your personal thoughts (and that I probably shouldn't worry too much about 'em). Once again, I value any feedback, no matter how small! Just skip any if you find 'em annoying.
1) Suppose we had a simple array, e.g. list of numbers. Then would Console.WriteLine(myArray) simply return its type? Or in other words, we have no choice but to use some kind of loop to display myArray?
2) Suppose we had a simple conditional statement, like so:
Code:
double x = Math.PI/3, result = 0; //you can get the method and divide it too
result = (x != 0.0) ? Math.Tan(x) : 1.0; //if true take first choice, o.w. take second choice
Console.WriteLine("Exact value = {0}", result);
I notice result=0, but it didn't seem necessary. So is it just good practice to do so? (Recall that the statement above is more or less an if-else type)
3) We can quickly resize an array and then assign the word "empty" to the null elements:
Code:
Array.Resize(ref yourArray, 6);
for (int i = 0; i < yourArray.Length; i++)
{
// Use the ?? operator to assign "empty" to null elements:
yourArray[i] = yourArray[i] ?? "empty";
Console.Write(yourArray[i] + " "); }
I was curious, but I was wondering how yourArray[i] = yourArray[i] ?? "empty" can recognize which array elements are null after resizing? Or is it behind the scenes?
4) If you declare an array (be it single, multi- or jagged) without initializing first, you must initialize it afterwards AND use the new operator. Is it because the elements were zero when we declared it? E.g.
Code:
int[] myIntArray; // Declaration
myIntArray = new int[] { 1, 3, 5, 7, 9 }; // Initialization
5) And finally, suppose we wanted to display or print the elements of a 2-dimensional array in a table. The book gives one way, but I notice another (see second statement):
Code:
// Two dim array:
int[,] myTwoDimArray;
myTwoDimArray = new int[3,2] { { 1, 2 }, { 3, 4 }, { 5, 6 } }
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 1; j++)
{
Console.Write(myTwoDimArray[i, j] + " ");
}
Console.WriteLine();
}
Alternatively,
Code:
// Two dim array:
int[,] myTwoDimArray;
myTwoDimArray = new int[3,2] { { 1, 2 }, { 3, 4 }, { 5, 6 } }
for (int i = 0; i <= 2; i++)
{
Console.Write("{0} ", myTwoDimArray[i,0]);
Console.WriteLine("{0}", myTwoDimArray[i,1]);
}
Anyhow, I was just curious if there is another way to display the table, i.e. 3x2. (I might try a foreach statement).
Anyhow, if I don't get any responses, thanks again for reading. Cheers!