Next Page



C# Language Fundamentals

If you are new to the C# School
This is the 3rd of a series of lessons of our C# school. C# School is a kind of interactive learning platform where those who want to learn .Net with C# can find help and support with an issue a week describing some areas of the C# Programming Language with the Microsoft .Net Platform. This is not the same traditional passive tutorial where the author only writes and the reader only reads. There will be exercise problems at the end of each issue which a reader is supposed to solve after reading the issue. The solution to these problems will be provided in the next issue for testing purpose. There is also a dedicated message board attached with the school where you can ask any questions about the article and the author will respond to your question within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved to either the Author ( farazrasheed@acm.org) or the WEBMASTER ( info@programmersheaven.com).

For previous lessons: Lesson Plan
Today we will learn the language fundamentals of C#. We will explore the data types in C#, using variables, operators, flow control statements like if.. else, looping structure and how to use arrays.

Basic Data Types and their mapping to CTS (Common Type System)
There are two kinds of data types in C#.
  • Value Types (implicit data types, structs and enumeration)
  • Reference Types (objects, delegates)
Value types are passed to methods by passing an exact copy while Reference types are passed to methods by passing only their reference (handle). Implicit data types are defined in the language core by the language vendor, while explicit data types are types that are made by using or composing implicit data types.

As we saw in the first issue, implicit data types in .Net compliant languages are mapped to types in the Common Type System (CTS) and CLS (Common Language Specification). Hence, each implicit data type in C# has its corresponding .Net type. The implicit data types in C# are:

C# type   .Net type  Size in  Description
                      bytes
bool      Boolean       1     Contains either true or false
char      Char          2     Contains any single Unicode
                              character enclosed in single
                              quotation mark such as 'c'
Integral types
byte      Byte          1     May contain integers from 0-255
sbyte     SByte         1     Signed byte from -128 to 127
short     Int16         2     Ranges from -32,768 to 32,767
ushort    UInt16        2     Unsigned, ranges from 0 to 65,535
int       Int32         4     Ranges from -2,147,483,648 to
(default)                     2,147,483,647
uint      UInt32        4     Unsigned, ranges from 0 to
                              4,294,967,295
long      Int64         8     Ranges from
                              -9,223,372,036,854,775,808 to
                              9,223,372,036,854,775,807
ulong     UInt64        8     Unsigned, ranges from 0 to
                              18,446,744,073,709,551,615
Floating point types
float     Single        4     Ranges from ±1.5 × 10-45
                              to ±3.4 × 1038 with 7 digits
                              precision. Requires the
                              suffix 'f' or 'F'
double    Double        8     Ranges from ±5.0 × 10-324 to
(default)                     ±1.7 × 10308 with 15-16 digits
                              precision

decimal   Decimal       12    Ranges from 1.0 × 10-28 to
                              7.9 × 1028 with 28-29 digits
                              precision. Requires the
                              suffix 'm' or 'M'


Implicit data types are represented in language using keywords, so each of the above is a keyword in C# (Keyword are the words defined by the language and can not be used as identifiers). It is worth noting that string is also an implicit data type in C#, so string is a keyword in C#. The last point about implicit data types is that they are value types and thus stored on the stack, while user defined types or referenced types are stored using the heap. A stack is a data structure that store items in a list in first out (LIFO) fashion. It is an area of memory supported by the processor and its size is determined at the compile time. A heap consists of memory available to the program at run time. Reference types are allocated using memory available from the heap dynamically (during the execution of program). The garbage collector searches for non-referenced data in heap during the execution of program and returns that space to Operating System.


                     Next Page



  User Comments


Anonymous

(Report as abusive)
India
A good Piece of work for refreshing the Language
Chris Roberts
(Not rated)
From Des Moines, IA
(Report as abusive)
Stacks are FILO
:A stack is a data structure that store items in a first in first out (FIFO) fashion.

Actually, I'm pretty sure stacks are first in last out (FILO).
Chris Roberts

From Des Moines, IA
(Report as abusive)
Conflicting Spec
Near the end, a code sample implies that a byte holds a value from -128 to +127. I think that's sbyte.
Anonymous
(Not rated)
(Report as abusive)
mr
A stack is a FILO that is as I know
Tri

From VietNam
(Report as abusive)
Great tutorial
This tutorial is easy to read and follow with enough information. Thanks.
  View all   Rate and comment this article




 
Printer friendly version of the les_csharp_3_p1 page


Sponsored links

Check For Updates
Easily add update features to your applications. A complete .Net updating solution.
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Online Crash Analysis
Automatically capture customer crash data, no debugger required. Support for .NET, C++, OS X, Java.
.Net Localization in three simple steps (WYSIWYG)
Localize .Net, C#/C/C++ & Delphi apps visually. HTML, HTML Help, XML & databases. Try Sisulizer now!


Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.