|
A modern programming language, like C#, requires all significant pieces of code to be a part of a type. By quickly reviewing Lesson 1 of this tutorial, you will notice that every program is contained within a class element. Along with the class, C# supports the following types:
C# is a typed language; so all variable declarations must include a valid type. By comparison, JavaScript is not type, because all JavaScript variables are of the same type: Object |
C# can balance the need of using fast types with the need of using flexible types.
Fast types are called value types and are allocated on the stack. Stack is a memory space allocated by the compiler and cannot be changed at runtime. Stack space is managed as part of a current scope - a region of code where a given variable is declared.
Because stack memory space is pre allocated for any given scope, value types can be quickly allocated and do not need cleanup.
Flexible types are called reference types and are allocated on the heap.
Value types are faster to allocate, but they cannot be changed and need to be copied.
Reference type are slower to allocate but they can be changed - no need to reallocate them
Here are some basic rules to choose between value types and reference types:
Any C# program contains user defined types. User defined value type is called "class" and we have seen it in the Lesson 1 Hello World program. User defined value typed is called "struct" and it can always be used instead of the corresponding class.
Here is an example of Hello World program using structure as a user defined type
using System;
struct Test{
static void Main()
{
System.Console.WriteLine("Hi there");
}
}
Integer is the simplest value type - it is most intuitive to beginners and is familiar from other languages. Integers support standard operations of multiplication, subtraction, addition and division.
Here is an example of decimal integers.
int x=2;
int y=3;
int z=x+y;//5
Hexadecimal integers are declared with "0x" in front of the number
int x=0xA;
int y=0xB;
int z=x+y//0x15
Another interesting value type is enumeration. Enumeration provides a convenient place holder for various constants and greatly improves readability of the C# code.
enum ReturnCodes {OK,FAILED,UNKOWN};
We will talk more about code readability in LessonCodeReadability.
C# String is a built-in reference type. Strings support a cornucopia of operations. They can be concatenated with a "+" and compared with ==.
string FirstName="Aleksey";
string LastName="Nudelman";
string Name=FirstName+" "+LastName;
You can replace substrings in a string like this
string newString=Name.Replace("l","xxx");//Axxxeksey Nudexxxman
It is important to note that C# strings are immutable. Every time a string is changed, a new string needs to be allocated to contain the result. Basically C# string behaves like a value type rather than a reference type. We will discuss C# string performance in LessonOptimization
Array is the most primitive reference type. To declare an array we provide it's type followed by [] and the name.
int []x;
All elements of the array must belong to the same declared type. The size of the array should be available at the time of array's creation
x=new int[5];
Each element of an array of value types is allocated on the stack and is immediately accessible
x[3]=5;
However, allocating an array of reference types, does not allocate individual elements on the heap - each element needs to be individually allocated.
Test []x=new Test[6];
for( int i=0; i<6; i++)
x[i]=new Test(i);
C# supports typeless array through an array of objects:
object []arr={1,"Hi there", Test};
You can store absolutely any kind of data as an array:
Array is the preferred data structure for a sequential data of fixed size, but has only rudimentary manipulation routines and needs to be reallocated as the size of data grows.