Data is physically stored inside cells of memory. This memory could be physical memory (Hard disk) or logical memory (RAM). Any cell of memory is represented with a unique address. This address is more than some combination of numbers or symbols.
C# language provides for practically all the data types. These types can be divided in three categories: value types, reference types and pointer types.
There are some more basic concepts to be learnt before the discussion of the data types. This is about variables and constants. A Variable is a named cell of memory used for data storage. A Variable value can be changed anytime. Every variable must have a type and this type must be set before it is used. Qualifying a variable with a type is called as declaration of variable. The type of a variable is the most important aspect and it defines the behavior of variable. All variables can be divided into seven main categories depending on the context of usage:
- Static variables
- Variable of instance
- Array’s elements
- Parameters given by reference
- Parameters given by value
- Returned values
- Local variables.
Static Variables will be alive throughout the life of a program. It can be declared using static modifier.
An Instance variable is a variable declared without static modifier. Usually it is declared inside a class or structure definition.
Parameter Values can be viewed as input parameters into methods:
public static void Sum(int a, int b) { Console.WriteLine(“The sum of elements {0} and {1} is {2}”,a,b,a + b); } |
This code writes in console values of variables a, b and their summary value. Now if the values of these parameters are modified inside the method, this change will not be reflected inside the main function. It is because the compiler creates copies of them when it passes them as value types. This ensures that their original values are not modified.
Instead if one wants to modify the parameter variables inside the function, C# has something called Reference variables. Reference variables also have a modifier out which can be used before their type. Look at the following example:
public static void SumRef(ref int a, ref int b) { a = 4; b = 6; Console.WriteLine(“The sume of elements {0} and {1} is {2}”,a,b,a + b); } |
Now this method modifies the value of variables a and b with values 4 and 6. These values are retained even after the execution of the function gets completed.
If the parameters need to be returned then they can be qualified with out modifier or as returned parameter in method definition. Here is an example of both of them, in which both of them return the same value:
public static int SumOut(int a, int b, out int sum1) { sum1 = a+b; Console.WriteLine(“The sum1 of elements {0} and {1} is {2}”,a,b,a+b); return sum1; } |
In main function it must be called in the next manner:
int sume ; sume = SumeOut(2,2, out sume);
Constants in C#:
Constant type of data cannot be changed. To declare a constant the keyword const is used. An example for the constant declaration is: const double PI = 3.1415;
Values types in C#:
Value type stores real data. When the data are queried by different function a local copy of it these memory cells are created. It guarantees that changes made to our data in one function don’t change them in some other function. Let see at a simple example:
public class IntClass
{
public int I = 1;
}
Here we have simple class that contains only one public data field of integer type. Now have a look on its usage in main function:
static void Main(string[] args) { // test class int i = 10; int j = i; j = 11; IntClass ic1 = new IntClass(); IntClass ic2 = ic1; ic2.I = 100;Console.WriteLine(“value of i is {0} and j is {1}”,i,j); Console.WriteLine(); Console.WriteLine(“value of ic1.I is {0} and ic2.I is {1}”,ic1.I,ic2.I); Console.WriteLine(); } |
Reference Types in C#:
In the above example, assume that First we have two value type i and j. Also assume that the second variable is initialized with the value of the first one. It creates new copy in memory and after it the values of these variables will be next:
i = 10;
j = i;
There are a few more things written in the above example for explaining the Reference Types in C#. At first, the variable ic1 of IntClass is created using dynamic memory allocation. Then we initialize the variable ic2 with value of ic1. This makes both the variables ic1 and ic2 referring to the same address. If we change a value of ic2, it automatically changes the value of ic1.
Now, over to the discussions about the important value types used in C#. The category simple types contains some predefined or system types that also are commonly used in other programming languages. It contains integer types: byte, Sbyte, Long, Ulong, Short, Ushort, int, Uint. These common types differs only range of values and sign.
Next simple type is character type. To declare a variable of this type need use keyword char. It can take values of characters or numbers as 16-digit symbol of the type Unicode.
The Boolean type has only two values: true, false. But these values cannot be assigned with a 0 or 1 as in C++ language.
Next category of simple types is floating point types. It contains two types float and double. Float type can get values in range from 1.5*10-45 to 3.4*1038. Double type has range of values from 5.0*10-324 to 1.7*10308.
A structural value types are struct and enum. Struct is a the same as class but it uses real values not references. The following code snippet contains the definition for struct:
struct Point3D { public float m_x; public float m_y; public float m_z;public float [] GetArray() { float [] arr = new float[3]; arr[0] = m_x; arr[1] = m_y; arr[2] = m_z; return arr; } } |
The above is declaration for a simple structure of real 3D point. As you see a class declaration looks very similar to the struct except that the class also has a constructor.
Enumerated types can be used to create some set of identifiers that can have values of simple type. Let us see at example of enum type:
public enum Days { Monday, Tuesday, Wensday, Thursday, Friday, Saturday, Sunday } |
In example there are enum that has days of week names. The values of days by default are in range from 0 to 6.
Common types in C#:
Object in C# language is universal; it means that all supported types are derived from it. It contains only a couple of methods: GetType() – returns a type of object, ToString() returns string equivalent of type that called.
Next type is class. It is declared in the same manner as structure type but it has more advanced features.
Interface is an abstract type. It is used only for declaring type with some abstract members. It means members without implementations. Please, have a look at piece of code with a declaration for a simple interface:
interface IRect { int Width { get; set; }int Height { get; set; }int CalculateArea(); } |
The members of interface can be methods, properties and indexers.
Next reference type to be dealt is delegate. The main goal of delegate usage is encapsulation of methods. It most like at pointers to function in C++.
String is common type that is used in almost all programming languages of high level. An example of string declaration and initialization:
string s = “declaration and init”;
The last very used reference type is array. Array it is set of elements that have the same type. Array contains list of references on memory cells and it can contain any amount of members. In C# there are three types of arrays: one-dimensional, two-dimensional and jagged array.
So, this covers almost all types used in C#. All these types can be cast to another type using special rules. An implicit casting can be done with types when values of variables can be converted without losing of any data. There is special type of implicit casting called boxing. This enables us to convert any type to the reference type or to the object type. Boxing example:
// boxing char ch = ‘b’; object obj = ch; Console.WriteLine(“Char value is {0}”,ch); Console.WriteLine(“Object value is {0}”,obj); Console.WriteLine(); |
This piece of code prints the same values of integer type variable and object type variable. The opposite process to the boxing is un-boxing. An example for un-boxing is as follows.
// unboxing float q = 4.6f; object ob = q; Console.WriteLine(“Object value is {0}”,ob); float r = (float)ob; Console.WriteLine(“Float value is {0}”,r); |
So, it is main item of common data type creating and using. All sources are attached. To compile and run it need to run .NET command line. Just type: csc DataTypes.cs. It creates DataTypes.exe that can be run as standard executable file. You can download the sample code here.