none
Is there a way to change the array numbers to anything else then int32? RRS feed

  • Question

  • When i create an array (a string array for this example) the numbers inside of the array that represents the items inside them are Int32 by default how can i change that?

        string[] strArray = new string[2] { null, null };
        strArray[0] = "a";
     
    That 0 for example is an int32 by default.
    Friday, January 11, 2019 4:34 AM

Answers

  • "When i hover my mouse over [256] it says its still int32."

    That's because an integral literal in C# (and C) is int32. Note that even in an embedded system this will be true. I'm not aware of any embedded system that is an 8-bit system although they may exist. You aren't actually optimizing anything here. Even if you declare a byte variable (which by the way only goes to 255) and assign it 255 the literal is integral. The stack allocation is still 4 bytes (on 32-bit systems). The size of the local variable doesn't matter at all in this case.



    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Andrew B. Painter Tuesday, January 15, 2019 12:19 AM
    • Marked as answer by xanrer Tuesday, January 29, 2019 12:36 PM
    Monday, January 14, 2019 4:39 PM
    Moderator
  • Greetings Xanrer.

    I think what Viorel and CoolDadTx are trying to tell you is that the index (the bit in the square brackets) is not stored with the array. So it is pointless trying to get the compiler to treat it as a byte, because that will not save any space.

    An array is as efficient as it gets. Don't use a dictionary, don't use anything else, just use the array as you are.

    • Marked as answer by xanrer Tuesday, January 29, 2019 12:37 PM
    Monday, January 14, 2019 10:12 PM

All replies

  • Consider a Dictionary. For example, to use strings instead of Int32:

       Dictionary<string, string> d = new Dictionary<string, string>();

       d["some index"] = "a";

       d["other index"] = "b";

       Console.WriteLine( d.Count );

       Console.WriteLine( d["some index"] );

     

    Friday, January 11, 2019 5:53 AM
  • When i create an array (a string array for this example) the numbers inside of the array that represents the items inside them are Int32 by default how can i change that?

        string[] strArray = new string[2] { null, null };
        strArray[0] = "a";
     
    That 0 for example is an int32 by default.

    Change it to what? Another numeric type? A string? Other?

    Why do you want that capability? What *exactly* are you trying to do with
    the array?

    - Wayne

    Friday, January 11, 2019 8:57 AM
  • On an array, no. Arrays are zero-based indexed by integral values.

    However you can create your own type that has an indexer that uses whatever type you want. We already have List<T> and Collection<T> for integral values and Dictionary for any other type so they should pretty much cover any situation you need.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, January 11, 2019 3:13 PM
    Moderator
  • When i create an array (a string array for this example) the numbers inside of the array that represents the items inside them are Int32 by default how can i change that?

        string[] strArray = new string[2] { null, null };
        strArray[0] = "a";
     
    That 0 for example is an int32 by default.

    Change it to what? Another numeric type? A string? Other?

    Why do you want that capability? What *exactly* are you trying to do with
    the array?

    - Wayne

    Yes i'm trying to change it to byte instead of int32, i don't need that much data space, so its just for optimization.
    Saturday, January 12, 2019 1:41 PM
  • You cannot change the indexer of the array, but you can define your own class that, when seen from outside, behaves like an array with an index type of your choice. To do that, you write inside your class something that is called the "indexer". It looks like a property, but the name of the property is "this", and it takes parameters between square brackets. In the get and set you write your own code, which returns or saves values in accordance to the value that you received as an index.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/

    Saturday, January 12, 2019 2:00 PM
    Moderator
  • Consider a Dictionary. For example, to use strings instead of Int32:

       Dictionary<string, string> d = new Dictionary<string, string>();

       d["some index"] = "a";

       d["other index"] = "b";

       Console.WriteLine( d.Count );

       Console.WriteLine( d["some index"] );

     

    How can i do this with byte or int16?
    When i do this:
                    Dictionary<string, byte> buds = new Dictionary<string, byte>();
                    buds[2] = "";
    It says that [2] is still an int32.

    Also for feature referance, lets say i want to make a byte array that has byte amount of things inside what collection should i use for that? ("collection?"<byte, byte>)
    Saturday, January 12, 2019 2:20 PM
  • Yes i'm trying to change it to byte instead of int32, i don't need that much data space, so its just for optimization.
    You may find the discussions and samples here to be informative - Character Encoding in .NET
    Saturday, January 12, 2019 4:27 PM
  • [...]

     lets say i want to make a byte array that has byte amount of things inside what collection should i use for that? ("collection?"<byte, byte>)

    I think that you can use this array:

       byte[] myArray = new byte[256];

    For indexing you can use byte or int (between 0 and 255). The type of index does not influence the allocated space of this array.



    Saturday, January 12, 2019 5:11 PM
  • Update: Viorel's point is correct. The index being used is irrelevant of what is being stored. The discussion that follows is a general view of pre-mature optimization.

    It isn't really an optimization. That's an old embedded programmer thought process. Modern processors are 64-bit which means they are optimized to access 64-bit blocks. Whether you read 1 byte or 8 it is the same effort.

    The difference between a byte and int is 3 bytes. So every place in your code you would normally use an int you'll need to cast to a byte which means lots of casting going on. The savings is 3 bytes. Ignoring the extensions for working with larger than 2GB arrays, Count is 32-bits which means you can have up to 2 billion elements in the array. This is highly unrealistic so a far more reasonable estimate might be a couple of hundred before you should consider paging. But lets go with 1000 elements. 1000 bytes is approximately 1K, 1000 ints is approximately 4K. So on a, very large, array you're saving 3K. 3K when you're talking about processes that are using 100+MB of memory is completely irrelevant.

    Furthermore the paging size of most processors is at least 4K. The current Intel processors have a minimum page size of 4K. So to read your entire array into memory would take 1 read from the processor. Whether it was 1K or 4K is completely irrelevant.

    Moral of the story - do not make your code more complex by trying to optimize code that doesn't actually need to be optimized. The rule of thumb is optimize the 20% of your code that runs 80% of the time. The only way to know that is to run a profiler. Pre-mature optimization adds unneeded complexity to code, requires careful documentation so it isn't undone and, in my experience, either has no impact on the performance at all or negatively impacts it.


    Michael Taylor http://www.michaeltaylorp3.net



    Saturday, January 12, 2019 5:18 PM
    Moderator
  • On an array, no. Arrays are zero-based indexed by integral values.

    However you can create your own type that has an indexer that uses whatever type you want. We already have List<T> and Collection<T> for integral values and Dictionary for any other type so they should pretty much cover any situation you need.


    Michael Taylor http://www.michaeltaylorp3.net

    Can you open this more with an example?
    Sunday, January 13, 2019 2:40 PM
  • Hi xanrer,

    Thank you for posting here.

    >> When i create an array (a string array for this example) the numbers inside of the array that represents the items inside them are Int32 by default how can i change that?

    If you want to use custom index,  we could use Dictionary<TKey,TValue>  to achieve it, here is a simple sample for your reference.

    static void Main(string[] args)
            {
                Dictionary<byte, string> b = new Dictionary<byte, string>();
                b[0x00C9] ="hello";
                Console.WriteLine(b[0x00C9]);
                Console.ReadKey();
            }
    

    Hope my suggestion could be helpful.

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 14, 2019 8:36 AM
    Moderator
  • Update: Viorel's point is correct. The index being used is irrelevant of what is being stored. The discussion that follows is a general view of pre-mature optimization.

    It isn't really an optimization. That's an old embedded programmer thought process. Modern processors are 64-bit which means they are optimized to access 64-bit blocks. Whether you read 1 byte or 8 it is the same effort.

    The difference between a byte and int is 3 bytes. So every place in your code you would normally use an int you'll need to cast to a byte which means lots of casting going on. The savings is 3 bytes. Ignoring the extensions for working with larger than 2GB arrays, Count is 32-bits which means you can have up to 2 billion elements in the array. This is highly unrealistic so a far more reasonable estimate might be a couple of hundred before you should consider paging. But lets go with 1000 elements. 1000 bytes is approximately 1K, 1000 ints is approximately 4K. So on a, very large, array you're saving 3K. 3K when you're talking about processes that are using 100+MB of memory is completely irrelevant.

    Furthermore the paging size of most processors is at least 4K. The current Intel processors have a minimum page size of 4K. So to read your entire array into memory would take 1 read from the processor. Whether it was 1K or 4K is completely irrelevant.

    Moral of the story - do not make your code more complex by trying to optimize code that doesn't actually need to be optimized. The rule of thumb is optimize the 20% of your code that runs 80% of the time. The only way to know that is to run a profiler. Pre-mature optimization adds unneeded complexity to code, requires careful documentation so it isn't undone and, in my experience, either has no impact on the performance at all or negatively impacts it.


    Michael Taylor http://www.michaeltaylorp3.net



    I completely see your point however im using c# as a training program for myself, im really into dos era programming yet i had little to no experience on algorithm before i started c#.
    At the time i didn't really care about dos era programming so i choose c# as a starter (also small basic before that).
    And now since i want to go back to dos programming (which im gonna start c++(dos) shortly after im done with my c# projects on hand) im just challenging myself about "How optimized i can make this program" (which im failing miserably). So that's why im running after every small detail.
    For short every single byte meters for me.
    And thank you for your detailed answer!
    Monday, January 14, 2019 3:29 PM
  • [...]

     lets say i want to make a byte array that has byte amount of things inside what collection should i use for that? ("collection?"<byte, byte>)

    I think that you can use this array:

       byte[] myArray = new byte[256];

    For indexing you can use byte or int (between 0 and 255). The type of index does not influence the allocated space of this array.



    When i hover my mouse over [256] it says its still int32. Thats what im trying to change to byte. Please pardon me if i understand this wrong but, will that array being int32 instead of byte will change even a few bytes of memory or not?
    Monday, January 14, 2019 3:53 PM
  • "When i hover my mouse over [256] it says its still int32."

    That's because an integral literal in C# (and C) is int32. Note that even in an embedded system this will be true. I'm not aware of any embedded system that is an 8-bit system although they may exist. You aren't actually optimizing anything here. Even if you declare a byte variable (which by the way only goes to 255) and assign it 255 the literal is integral. The stack allocation is still 4 bytes (on 32-bit systems). The size of the local variable doesn't matter at all in this case.



    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Andrew B. Painter Tuesday, January 15, 2019 12:19 AM
    • Marked as answer by xanrer Tuesday, January 29, 2019 12:36 PM
    Monday, January 14, 2019 4:39 PM
    Moderator
  • Greetings Xanrer.

    I think what Viorel and CoolDadTx are trying to tell you is that the index (the bit in the square brackets) is not stored with the array. So it is pointless trying to get the compiler to treat it as a byte, because that will not save any space.

    An array is as efficient as it gets. Don't use a dictionary, don't use anything else, just use the array as you are.

    • Marked as answer by xanrer Tuesday, January 29, 2019 12:37 PM
    Monday, January 14, 2019 10:12 PM
  • "When i hover my mouse over [256] it says its still int32."

    That's because an integral literal in C# (and C) is int32. Note that even in an embedded system this will be true. I'm not aware of any embedded system that is an 8-bit system although they may exist. You aren't actually optimizing anything here. Even if you declare a byte variable (which by the way only goes to 255) and assign it 255 the literal is integral. The stack allocation is still 4 bytes (on 32-bit systems). The size of the local variable doesn't matter at all in this case.



    Michael Taylor http://www.michaeltaylorp3.net

    Oh.. i see, well i tried :d
    So doing this is simply impossible in c#, i guess if any moderator there reading this, you can mark this question as halted.

    Thank you for explaining this.

    • Edited by xanrer Monday, January 14, 2019 10:36 PM
    Monday, January 14, 2019 10:31 PM
  • I'm having trouble viewing this thread, so this is a test post. Please ignore.
    Monday, January 14, 2019 11:36 PM
  • Greetings Xanrer.

    I think what Viorel and CoolDadTx are trying to tell you is that the index (the bit in the square brackets) is not stored with the array. So it is pointless trying to get the compiler to treat it as a byte, because that will not save any space.

    An array is as efficient as it gets. Don't use a dictionary, don't use anything else, just use the array as you are.

    Got it! Thanks.
    Tuesday, January 15, 2019 6:07 PM

  • Same issue.  Happens to me with a lot of threads.  I post and then hours and hours later I look back at the thread and find other posts that are timestamped ahead of mine.


    If you haven.r already done so, you may want to post your problems here:

    Forums Issues (not product support)
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=reportabug&filter=alllanguages

    - Wayne

    Tuesday, January 15, 2019 8:31 PM
  • I already posted an issue for this if anyone wants to chime in. It started last week for me.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 15, 2019 8:36 PM
    Moderator
  • Hi ,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 29, 2019 8:50 AM
    Moderator