C#. Base types in C#




Base types (values types) in C#


Questions


Search other websites:

Answers

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?

Answer:

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)?

Floating point data types operate on numeric values from a set of real numbers. There are three main data types in C# for representing floating point numbers (real numbers):

  • float – implements single precision numbers. This type is an alias for the System.Single class;
  • double – implements double precision numbers. This type corresponds to the System.Double class;
  • decimal – designed to perform calculations that require special precision in the representation of the fractional part of a number, as well as to minimize rounding errors. The decimal data type is useful for financial calculations, where the issue of precision is especially important.

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

2.85
-1000.399
20390239.939

Compared to double, decimal does not solve rounding errors, but minimizes them. Rounding errors occur when a number division operation occurs, the result of which gives a fractional part in the period

1.0/3.0 = 0.33333(3) - number 3 in the period

When this number 0.33333(3) is multiplied by 3, the original number 1.0 will no longer be obtained. Thus, a loss of precision occurs. The decimal type is designed to reduce this loss of precision by increasing the number of decimal places in a number.

 


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.

Example:

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:

'A'
'4'
';'

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

 


19. An example showing the difference between double and decimal types

The following example demonstrates the rounding difference between double and decimal.

When dividing 1.0 by 3.0, both types lose precision, which is natural. When the result is multiplied by 3.0, the following occurs:

  • for the double type, the result becomes 1.0. This means that the compiler has rounded the number 0.3(3). Such rounding in some cases (for example, financial calculations) is undesirable;
  • for the decimal type, the result becomes 0.9(9). This means that the inverse multiplication operation for the decimal type is performed without rounding. This means that the decimal type reduces precision loss errors for real numbers as compared to the double type.

The demo program text is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication8
{
  class Program
  {
    static void Main(string[] args)
    {
      // Difference between double and decimal types
      // 1. Declaring variables of types double and decimal
      double dbl;
      decimal dcm;

      // 2. Division 1.0/3.0 - loss of precision occurs for both types
      dbl = 1.0 / 3.0; // dbl = 0,333333333333333
      dcm = Decimal.One / (decimal)3.0; // dcm = 0,3333333333333333333333333333

      // 3. Displaying the result of division
      Console.WriteLine("dbl = {0}", dbl);
      Console.WriteLine("dcm = {0}", dcm);

      // 4. Multiplication by 3.0,
      //    the double type does rounding,
      // that is, there is a loss of precision in this direction (when multiplied)
      dbl = dbl * 3.0; // dbl = 1.0

      // the decimal type does not round,
      // there is no rounding, which means there is no loss of precision
      dcm = dcm * (decimal)3.0; // dcm = 0,9999999999999999999999999999

      // 5. Display the result for checking
      Console.WriteLine("dbl = {0}", dbl);
      Console.WriteLine("dcm = {0}", dcm);

      Console.ReadKey();
    }
  }
}

The result of the program

dbl = 0,333333333333333
dcm = 0,3333333333333333333333333333
dbl = 1
dcm = 0,9999999999999999999999999999