# Thread: 5 short questions ٩(͡๏̯͡๏)۶

1. ## 5 short questions ٩(͡๏̯͡๏)۶

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!

2. 1) Try it and see? If you do:
Code:
int[] array = new int[] { 2, 3, 7 };
Console.WriteLine(array);
.... you'll get "System.Int32[]".

2) You don't need the 0 initializer.

3) The ?? operator takes the first operand's value unless it's null, in which case it takes the second operand's value. So basically it's the same as:
Code:
if(yourArray[i] == null)
yourArray[i] = "empty"

3. 4) This is because you need to assign memory to the object/variable. The "new" operator does this. You could leave those parentheses empty and assign them later on, it's just the language supports you doing three main things when creating a new object/variable:

1: int myInt;
2: int myInt = new int();
3: int myInt = 5;

In example one you are just creating the variable for use in your code. This will throw out errors if you try and use it in something like int result = myInt * 4;. In example two you have created the variable and assigned memory space. However you have not assigned a value to it and you could get some very interesting results because the CLI is only referring to that space of memory and just like a piece of deleted data from a hard drive, what used to be there still exists. Example three creates the variable for use in code, assigns a value whilst under the cover creating the memory space needed to hold it.

4. Originally Posted by DanFraser
In example two you have created the variable and assigned memory space. However you have not assigned a value to it and you could get some very interesting results because the CLI is only referring to that space of memory and just like a piece of deleted data from a hard drive, what used to be there still exists.
That won't happen in C#. All intrinsic data types support a default constructor that will assign a default value to a variable of that type. On the case of int, that's 0. So the second example declares and defines a variable named myInt of type int and with the value 0.

5. excellent feedback as usual!!!!
3) Good point! I'm not sure if it clashes with my interpretation of, let's say,
Code:
int? myInt = null;
int yourInt = myInt ?? -22;
where the left-hand-side of ?? should be null to begin with. Nonetheless, I like your explaination, as well as everyone elses' clarifications. God bless!

6. Another clarification regarding question 3 (You ask how does it know if an array entry is null):

Code:
string[] myArray = {"hello", null, "world", "!"};
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] = myArray[i] ?? "empty";
Console.Write(myArray[i] + " ");
}
When you compile code like this, the compiler will transform your code into CIL language. You can use tools like ilasm or ILSpy to inspect the resulting IL code after building your application. The relevant instruction your will find there is brtrue.s. This instruction reads the value in the evaluation stack and if it is non null, true or non zero moves to a specific instruction address where that value is to be processed. Otherwise does nothing.

So, what you will see is something like this:

Code:
dup
pop
ldstr "empty"
stelem.ref
• dup will push a value into the evaluation stack,
• then brtrue.s tests this value and realizes it is null. Does nothing and moves to the next instruction, which on this case is pop which pops out the value from the evaluation stack. ldstr now loads the "empty" string into memory and stelem.ref will copy that string into the object reference (on this case your array of strings at the current index location).
• If brtrue.s realizes instead this value is not null, not 0 and not false, then it will instead move the execution pointer to the address that it points to. The code will run from there, ignoring all the instruction in between. That code will be the set of instructions that will eventually lead to the execution of Console.Write().

If you start asking yourself question like "What's happening in the backstage", keep your studies of C# for now, but remember those answers are given by looking at the IL code that the compiler generates from your code. In due time you'll be ready to start delving into IL. It's not absolutely necessary you do, but I can guarantee it will be a natural step you will eventually want to take once you get more comfortable with the C# language.

7. Although I admit your in-depth reply is a bit beyond me, you are definitely giving me a much more comprehensive understanding. Much obliged, friend! I will definitely refer back to your post!