The Best C# Programmer In The World - Ben Perkins Member Login  | Newsletter | RSS Feeds


 
 
 
TheBestCSharpProgrammerInTheWorld.com 
 
The Best C# Programmer In The World - Ben Perkins
Data Types in C#  
 
Data Types in C#
 
There are some really good descriptions and explainations concerning data types on the internet. However I will add my understanding and comments about them on this website. Just in case you stumble across this page in your search for ultimate C# knowledge.
 
There are 2 data types. Value Types and Reference Types. I published a training video that discusses these and the relation they have with the Stack and the Heap. Check it out here.
 
The trigger that prompted me to write this article is becuase I was searching for the differences between the numeric Value Types. I was a little confused about the difference between a decimal, integer, float, double, long, , etc... So I thought I'd write it down. I learn best when I write things down. All of your numberic data types are value types (watch my video, if a value type is part of class then they are still value types, but are stored like reference types). As well, a boolean, enum and struct are value types too. So in my quest to decide which numeric data type to use in which situation I discovered this table. Using it, I can take my current requirements into consideration and use the appropriate one.
 
C# Type .Net FrameworkSigned?Possible Values
sbyteSystem.SbyteYes-128 to 127
shortSystem.Int16Yes-32768 to 32767
intSystem.Int32Yes-2147483648 to 2147483647
longSystem.Int64Yes-9223372036854775808 to 9223372036854775807
byteSystem.ByteNo0 to 255
ushortSystem.Uint16No0 to 65535
uintSystem.UInt32No0 to 4294967295
ulongSystem.Uint64No0 to 18446744073709551615
floatSystem.SingleYes~ ±1.5 x 10-45 to ±3.4 x 1038 with 7 figures
doubleSystem.DoubleYes~ ±5.0 x 10-324 to ±1.7 x 10308 with 15 or 16 figures
decimalSystem.DecimalYes~ ±1.0 x 10-28 to ±7.9 x 1028 with 28 or 29 figures
charSystem.CharN/AAny Unicode character (16 bit)
boolSystem.BooleanN/Atrue or false
 
If you decide to use a short variable, take note that if the number exceeds 32767 an exception will happen. The trigger I refered to above was also mostly focused on fractional numbers. I.e. numbers to the right of the decimal point. Therefore I was and am very interested in explaining the difference between a float, double and a decimal.
 
As you can see from the table able a float is signed, meaning it can be positive or negative and will provide us with 7 numeric values to the right of the decimal point. A double is an exponetially larger value than the float and the decimal and will provide us with 15-16 numeric values to right of the decimal point. The decimal, provides us with the largest number of significant figures, being 28-29. Therefore, if you are after the greatest amount of numeric values to the right of the decimal, then use a decimal. However, the largest number can be stored into the double value type.
 
 
 
Feedback / Question
 
Your Name:Your Email:
 
Subject:
 
Feedback/Question:
 
 
 
I had to remove the capability to leave feedback due to this. Will be back soon.
 
 
page.Translate()
 
 
blog.Stats()
 
  Posts: 113
  Comments: 86
  Fundamentals: 16
 
my.Book()

 
me.About()
 
 
 
 
 
blog.Archive()
 
2012 May  (4)
2012 April  (5)
2012 March  (4)
2012 February  (4)
2012 January  (5)
2011 December (2)
2011 November (6)
2011 October (7)
2011 September (7)
2011 August (9)
2011 July (9)
2011 June (8)
2011 May (9)
2011 April (7)
2011 March (9)
2011 February (8)
2011 January (8)
2010 December (7)
2010 November (8)
2010 October (4)
 
site.Visits()
 
free counters
 
tag.Cloud()
 
code.Disclaimer()
 
The sample code on this website is provided to illustrate a concept and should not be used in applications or Web sites without proper professional consultation, as it may not illustrate the safest coding practices. I assume no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
 
   


The Best C# Programmer In The World - Ben Perkins, © 2010, All Rights ReservedContact Ben