Deciding which type to use?

Posted on 2004-11-18
Last Modified: 2010-04-15
Hi there,

I was wondering which is the best type to use, let me explain... When ever i am using an integer for example then I would generally use int (int32) for example

But what if i need to only store either 0 or 1, then is it better to use a Byte type rather than another type or an int?

This does seem pretty logical to me but before making mistakes i thought i would ask.

Basically the rule should be use the smallest int possible or use a byte etc..., takes up less space in memory but what about optimisation, using a byte wouldn't take more time to run than using say an int (int32)??

Just wanted some clarification


Question by:ianinspain
    LVL 48

    Expert Comment

    For variable which can be 0 or 1 consider using bool (System.Boolean) type.
    However, if you want to use this variable in arithmetic (not logical) expressions, use int or byte.
    Using smallest possible type saves memory, but sometimes may reduce performance. For example, processor doesn't like unaligned addresses and array of integers may be handled faster than array of bytes. This depends on situations where this variable will be used.

    Author Comment

    Thanks AlexFM,

    This is what I thought, so I suppose its a trade off of weather I want to save memory or performance.

    Are there any set rules?

    Or should i just always use the smallest type possible for the calculation? or just use ints ??

    The reduce in performance is worth the save in memory or visa versa?

    LVL 48

    Accepted Solution

    It depends on situation where you want to use this variable.

    1) Variable is used in logical expressions ( if ... ). Use Boolean type.

    2) Variable is used in math expressions. Use int or byte type.

    If you want to use this variable in large arrays, use byte type. For single variable it doesn't matter.

    int x[1000000];


    byte x[1000000];

    byte is better. A lot of memory wasted in first case reduces performance.

    int x;
    byte y;

    x += y;
    Possibly this performs better if y is int. But difference is so small, that you can see it if you put this statement in some loop.

    Additional considerations: not all parts of the program are subject of optimization. For example:


    for ( int i = 0; i < 1000000; i++ )

    Don't waste your time optimizing Function1. It doesn't matter whether it runs 10 ir 15 ms. However, Function2 must be optimized.

    Author Comment

    Much appreciated!

    That cleared up some concerns! Thanks
    LVL 48

    Expert Comment

    Processor doesn't like unaligned addresses. Single variable or class member will be aligned to 32-bits boundary. In this case, byte takes the same space and performs exactly like int. But array of bytes is better than array of integers, because array members are not aligned.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now