#280 – Implicitly-Typed Arrays

In the same way that you can declare an implicitly-typed variable, letting the compiler figure out the type of the variable’s value, you can declare implicitly-typed arrays.

In the example below, we declare and initialize both implicitly-typed variables and implicitly-typed arrays.  The compiler infers the type–shown in the associated comment.

            // Implicitly-typed variables
            var x = 42;         // int
            var s = "Hooey";    // string

            // Implicitly-typed arrays
            var someArray = new[] { 5, 6, 7 };             // int[]
            var arrB = new[] { new Dog("Bob"), null };     // Dog[]
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

8 Responses to #280 – Implicitly-Typed Arrays

  1. ocaccy says:

    Hi Sean, nice to meet you.

    Playing with arrays.

    With myArray1 [320,18] and myArray2 [8,18]

    How do this:

    myArray2 [0,0] = MAXIMUM (myArray1 [11,0] ~ myArray1 [17,0])
    myArray2 [1,0] = AVERAGE (myArray1 [18,0] ~ myArray1 [24,0])
    myArray2 [2,0] = AVERAGE (myArray1 [25,0] ~ myArray1 [75,0])
    myArray2 [3,0] = AVERAGE (myArray1 [76,0] ~ myArray1 [18,0])
    myArray2 [4,0] = AVERAGE (myArray1 [181,0] ~ myArray1 [320,0])
    ?
    Regards,
    ocaccy

    • Sean says:

      It’s not quite clear what your question is, or what you’re trying to do. Can you describe the problem more completely? Also, I’m not sure why you have the tilde (~) in your expressions – is this meant to be a minus (-) sign?

  2. ocaccy says:

    Thank you, Sean.

    With myArray1 [320,18] and myArray2 [8,18]

    How do this:
    myArray2 [0,0] = MINIMUM from here (myArray1 [11,0] to here myArray1 [17,0]
    myArray2 [1,0] = MAXIMUM from here (myArray1 [11,0] to here myArray1 [17,0])
    myArray2 [2,0] = AVERAGE from here (myArray1 [18,0] to here myArray1 [24,0])
    myArray2 [3,0] = AVERAGE from here (myArray1 [25,0] to here myArray1 [75,0])
    myArray2 [4,0] = AVERAGE from here (myArray1 [76,0] to here myArray1 [180,0])
    myArray2 [5,0] = AVERAGE from here (myArray1 [181,0] to here myArray1 [320,0])
    ?
    Regards,
    ocaccy

    • Sean says:

      Sounds like you’ll want to iterate through the elements in your array for which you want a minimum, maximum or average and calculate the relevant values. For minimum, just store the lowest value you find in that range of the array, for maximum the highest value and for average, sum up the elements and divide by the count.

      • ocaccy says:

        I have tried various solutions.
        I would like to see how would your solution to this.
        I have done as below but still very confusing to explain to my children.

        int max(int array[][WIDTH], int first_col, int last_col, int first_row, int last_row)
        {
        int i_max = array[first_col][last_col];

        for(int i = first_col; i <= last_col; i++)
        for(int j = first_row; j i_max)
        i_max = myArray[i][j];

        return i_max;
        }

        double average(int array[][WIDTH], int first_col, int last_col, int first_row, int last_row)
        {
        double the_avg = 0;

        for(int i = first_col; i <= last_col; i++)
        for(int j = first_row; j i_max)
        the_avg += myArray[i][j];

        the_avg /= ( (last_col – first_col + 1)*(last_row – first_row + 1) );

        return the_avg;
        }

  3. ocaccy says:

    Sean,
    how to convert string myArray[310,120] to int myArray[310.120]
    or
    string myArray[310,120] to int myArray2[310,120].

    Best Regards,
    ocaccy

  4. Pingback: #596 – Implicitly-Typed Arrays and Best Type Inference « 2,000 Things You Should Know About C#

  5. Pingback: #775 – Copying an Array of Anonymously-Typed Objects « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: