5 short questions ٩(͡๏̯͡๏)۶

This is a discussion on 5 short questions ٩(͡๏̯͡๏)۶ within the C# Programming forums, part of the General Programming Boards category; G'even gentlemen I know it hasn't been too long since my previous thread, but once again I'm a little bit ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    13

    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!
    Last edited by Minty; 05-29-2011 at 08:07 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    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"
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    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.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    Quote Originally Posted by DanFraser View Post
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    13
    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!
    Last edited by Minty; 05-29-2011 at 09:12 AM.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    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
    brtrue.s <some_address>
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    13
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. some super basic questions ٩(-̮̮̃ -̃)۶
    By Minty in forum C# Programming
    Replies: 5
    Last Post: 05-24-2011, 11:00 AM
  2. Replies: 14
    Last Post: 03-08-2010, 05:32 PM
  3. Replies: 7
    Last Post: 02-08-2008, 05:31 PM
  4. Short questions...
    By Devil Panther in forum C++ Programming
    Replies: 7
    Last Post: 09-03-2005, 02:47 PM
  5. A Few Short Questions....
    By SithVegeta in forum C Programming
    Replies: 6
    Last Post: 11-14-2004, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21