C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Datatypes in C# and .NET


Posted Date: 12 Apr 2008    Resource Type: Articles    Category: .NET Framework

Posted By: global       Member Level: Silver
Rating:     Points: 7




DataTypes are the basic building block of any language. Microsoft has tried to standardise the datatypes in .NET framework by introducing a limited, fixed set of types that can be used to represent almost anything in programming world.

C++ was very rich in datatypes, but that leads to confusion too. Especially, when you write components that may be consumed by applications written in other platforms, you have to make sure the types used are compatible with other platforms too!

.NET types start from a clean slate. All .NET languages share the same types. So, they are all compatible and no worries. This means, you can call C# code from VB.NET and vice versa, without worrying about type conversions.

.NET data types are either structures or classes, part of the System namespace. For example, the following data types are implemented as struct in .NET:
Int16
Int32
Double

(String is implemented as a class in .NET, for various reasons.)

If you are not very familiar with struct and class, don't worry about it. You can just use them as if they are simple data types.

Here is how you can declare variables of type Int, Double and String:


Int16 age, employeeNumber;
Double salary;
String name, address;

You can use any of the .NET data types directly in any .NET language - in C#, VB.NET or xyz.NET. But in addition to the .NET types, each language provides a set of primitive types, which map to the corresponding types in .NET class library. This is why you may see some people use string and some others use String. There is no big difference. string is a primitive data type in C# and String is the corresponding class in .NET class library. The string in C# is mapped to the class in .NET class library. So, whether you use string or String, there is no real difference.

DataTypes in C# and the corresponding class/struct in .NET class library

The following list shows the list of data types available in C# and their corresponding class/struct in .NET class library.

C# Data type Mapped to .NET class/struct
sbyte System.SByte
byte System.Byte
char System.Char
float System.Single
decimal System.Decimal
double System.Double
ushort System.UInt16
short System.Int16
uint System.UInt32
int System.Int32
ulong System.UInt64
long System.Int64
bool System.Boolean
string System.String
object System.Object


Value Types & Reference Types

In C# data types are classified into two :

value types
reference types

The following tables shows some of the differences between values types and reference types.

value types reference types
allocated on stack allocated on heap
a value type variable contains the data itself reference type variable contains the address of memory location where data is actually stored.
when you copy a value type variable to another one, the actual data is copied and each variable can be independently manipulated. when copying a reference type variable to another variable, only the memory address is copied. Both variables will still point to the same memory location, which means, if you change one variable, the value will be changed for the other variable too.
integer, float, boolean, double etc are value types. string and object are reference types.
struct is value type. classes and interfaces are reference types.




Responses

Author: @@@ Hyderabadi Biryani @@@    12 Apr 2008Member Level: Diamond   Points : 0
http://www.dotnetspider.com/tutorials/Datatypes.aspx


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sample C#.net program
Previous Resource: Points to remember about .Net
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

teleconferencing

Contact Us    Privacy Policy    Terms Of Use