Base types in C#

Base types (values types) in C#



1. What are the main (base) data types are implemented C#?

C# includes the following base data types:

  1. Integer types: sbyte, byte, short, ushort, int, uint, long, ulong.
  2. Types in which the stored data are as floating-point (corresponding to real type): float, double, decimal.
  3. The character type: char.
  4. Logical type: bool.
  5. String type: string.


2. What are the features of use of integer data types?

There are following integer data types in C#:

sbyte, byte, short, ushort, int, uint, long, ulong.

Data of these types are the values from the set of integer numbers. For example:

-200, 8938, 1134448348.

Symbols ‘u‘ before the titles of some types are the reductions by word unsigned. It means, the value of these types variables is unsigned, that is, you can’t to retain negative numbers in these types of variables.

The variables of types sbyte and byte occupy 1 byte of memory. Variables of types short and ushort2 bytes. Variables of types int and uint – 4 bytes. Variables of type long and ulong8 bytes.


3. How describe in the program a variable named ‘d‘ of integer type?


int d;

After this describing, the 4 bytes of memory will be allocated under variable ‘d‘.


4. How set the number 398 into the variable ‘d’ of ulong type?

To set the variable in any value is used the assignment operator, that is designated ‘=‘.

Answer 1. Assigning a value to a variable after its description.

ulong d;
d = 398;

Answer 2. Assigning a value to a variable after its description (initialization).

ulong d = 398;


5. How to programmatically determine the size of a variable of a given type?

To determine the size of variable is used sizeof() operation:

int d;
d = sizeof(sbyte); // d = 1
d = sizeof(short); // d = 2
d = sizeof(int);   // d = 4
d = sizeof(long);  // d = 8

In this way one can determine the size in bytes of any basic variable types.


6. What are the features of types of floating-point (real types)?

The floating-point data types represent the numeric values from the set of real numbers.

This means, that the types of floating point data can store the integer values.

In C# are three main floating-point data types.

float, double, decimal

These types allows to save the floating-point numbers, for example:



7. How to describe the corresponding real type variable (floating point)?

An example of describing of variables of type float, double, decimal:

float x;
double y;
decimal z;

Here is described the three variables named x, y, z. For the variable x is allocated 4 bytes of memory, for the variable y is allocated 8 bytes, for variable z is allocated 16 bytes.


8. How programmatically to set the value into a variable of corresponding real type?

An example how to enter a numeric data into the variables of different floating-point types:

float f = (float)9030.939; // initialization
double d;
decimal z;
d = -392093828.3993;
z = (decimal)390209.3099029;

In the code above, when data is entered in the variable f, value 9030.939 is converted into the float type. Same way, is converted the number for z variable into the decimal type. Such converting is required for types “float” and “decimal“, as all numeric values are of “double” type.


9. How to determine how many bytes of memory occupies a variable of type float, double or decimal?

To determine this, you need to type the following program code:

int d;
d = sizeof(float);   // d = 4
d = sizeof(double);  // d = 8
d = sizeof(decimal); // d = 16


10. How to convert the value from “float” type to “int” type?

To do this, is used an operation of type cast. In the brackets you need to set the type name to which you are cast.


float a;
int b;
a = (float)23.789;
b = (int)a; // b = 23

When the cast type operation is used, it is necessary to take into account the constraints that are imposed on the types, that take up less space in the computer memory.

For example, the variable of short type can be a smaller range, than variables of types float and double. In the following listing is an overflow in the values of type “short“:

short i;
float f;
f = (float)35748473.8;
i = (short)f; // i = 31352 - overflow


11. How convert the value from “int” type to “double” type?

An example of converting from “int” to “double“:

int i;
double d;
i = 3998;
d = (double)i;


12. What are the character data types?

Answer: char, string

Type string is a sequence of characters.

The variables of type char can get the value of one character. The value of a variable of type “char” is taken in single quotes, for example:


Data of type “char” is a single Unicode symbol. Such variable takes 2 bytes in the memory.

Variables of “string” type – this is a strings of characters, which are taken in double quotes, for example:

"This is a string"

The variable of string type in C# can be any length (number of symbols).


13. What are the features of the use of char data in the program?

Data of type char represent a symbol value of code, that was inputted from keyboard. The symbol code is an integer number. For example, the code of symbol ‘s’ is equal 115.

The code snippet, where symbol’s code is calculated:

int code;
char symbol;
symbol = 's';
code = (int)symbol; // code = 115

That is data of char type are the same integers. In the C#, the data of char type takes 2 bytes in the memory. C# operates with Unicode symbols. The character codes range is from 0 to 65535.


14. How get the character from the code?

The snippet of program code, which converts a code (integer value) into character (char type):

int code;
char symbol;
code = 115;
symbol = (char)code; // symbol = 's'


15. What are the features of using the variables of string type?

The variables of string type are the strings of characters. Maximum length of string is unlimited.

An example of describing of variable of string type named s1.

string s1;

An example of assignment a string into variables of “string” type:

string s1 = "Text-1";
string s2;
s2 = "Text-2";
char c;
c = s2[0]; // The symbol with the index 0. c = 'T'

You can apply different operations to the “string” type variables. The detailed description of most common operations used a variables of type “string” is described here.


16. What are the features of using the variable of “bool” type?

The variable of logical type “bool” defines only two states: true and false.

An example of using a “bool” type variable:

bool b1, b2 = true;
b1 = (6 <= 5); // b1 = false


17. An example of initialization of variables of the different types.
int d = 231;
float z = (float)-23.15;
char c = 'k';
string s = "Hello!";
double r = -32.593;
bool b = true;


18. How to determine a maximum value (minimum value) of variable of any type?

To determine a maximum value or minimum value of variable some type in the .NET Framework library are used properties MaxValue and MinValue.

An examples of determining the limit values of variables of different types.

For variables of type “int“:

// int type
int i;
int MaxI;
int MinI;
MaxI = int.MaxValue; // MaxI = 2147483647
MinI = int.MinValue; // MinI = -2147483648

For variables of type “ulong“:

// ulong type
ulong i;
ulong MaxUL;
ulong MinUL;
MaxUL = ulong.MaxValue; // MaxUL = 18446744073709551615
MinUL = ulong.MinValue; // MinUL = 0

For variables of type float:

// ulong type
float f;
float MaxF;
float MinF;
MaxF = float.MaxValue; // MaxF =  3.402823E+38
MinF = float.MinValue; // MinF = -3.402823E+38