Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using different numeric variable types

Tags:

variables

c#

Im still pretty new so bear with me on this one, my question(s) are not meant to be argumentative or petty but during some reading something struck me as odd.

Im under the assumption that when computers were slow and memory was expensive using the correct variable type was much more of a necessity than it is today. Now that memory is a bit easier to come by people seem to have relaxed a bit. For example, you see this sample code everywhere:

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

int? (-2,147,483,648 to 2,147,483,648) for length? Isnt byte (0-255) a better choice?

So Im curious of your opinion and what you believe to be best practice, I hate to think this would be used only because the acronym "int" is more intuitive for a beginner...or has memory just become so cheap that we really dont need to concern ourselves with such petty things and therefore we should just use long so we can be sure any other numbers/types(within reason) used can be cast automagically?

...or am Im just being silly by concerning myself with such things?

like image 776
Leroy Jenkins Avatar asked Mar 16 '10 02:03

Leroy Jenkins


People also ask

What are the numeric variable types?

The exact numeric types are INTEGER , BIGINT , DECIMAL , NUMERIC , NUMBER , and MONEY . Approximate numeric types, values where the precision needs to be preserved and the scale can be floating. The approximate numeric types are DOUBLE PRECISION , FLOAT , and REAL .

What is the use of numeric variable?

Numeric variables, as you might expect, have data values that are recognized as numbers. This means that they can be sorted numerically or entered into arithmetic calculations.

What are the 2 types of numeric variables?

Numeric variables may be either continuous or discrete.

What are the different types of numeric data type give example?

Examples of numeric data types are examination marks, height, weight, the number of students in a class, share values, price of goods, monthly bills, fees and others. In Visual Basic, numeric data are divided into 7 types, depending on the range of values they can store.


1 Answers

Luca Bolognese posted this in his blog.

Here's the relevant part:

  • Use int whenever your values can fit in an int, even for values which can never be negative
  • Use long when your values can't fit in an int.
  • Byte, sbyte, short, ushort, uint, and ulong should only ever be used for interop with C code. Otherwise they're not worth the hassle.
like image 199
Jason Down Avatar answered Sep 19 '22 03:09

Jason Down