C#
LECTURE
Abid Kohistani
Data Types
As explained in the variables chapter, a variable in C# must be a specified by data type In C#, there
are different types of variables (defined with different keywords), for example:
int myNum = 5; // Integer (whole number)
double myDoubleNum = 5.99D; // Floating point number
char myLetter = 'D'; // Character
bool myBool = true; // Boolean
string myText = "Hello"; // String
Why to use Data Types….
A data type specifies the size and type of variable values.
It is important to use the correct data type for the corresponding variable;
 to avoid errors,
to save time and memory
it will also make your code more maintainable and readable.
Common types of Data Types
Data Type Size Description
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
bool 1 bit Stores true or false values
char 2 bytes Stores a single character/letter, surrounded by single quotes
string 2 bytes per
character
Stores a sequence of characters, surrounded by double quotes
Numbers
Number types are divided into two groups:
Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are int
and long. Which type you should use, depends on the numeric value.
Floating point types represents numbers with a fractional part, containing one or more decimals. Valid types are
float and double.
Integer Types
Int: The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial,
the int data type is the preferred data type when we create variables with a numeric value.
Example :
int myNum = 100000;
Console.WriteLine(myNum);
◦ Long: The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807.
This is used when int is not large enough to store the value. Note that you should end the value with an "L":
Example :
long myNum = 15000000000L;
Console.WriteLine(myNum);
Floating point types
Float: The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the
value with an "F":
Example :
float myNum = 5.75F;
Console.WriteLine(myNum);
◦ Double: The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you can end
the value with a "D" (although not required):
Example :
double myNum = 19.99D;
Console.WriteLine(myNum);
The precision of a floating point value indicates how many digits the value can have after the decimal point. The
precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits.
Therefore it is safer to use double for most calculations.
Scientific Number
A floating point number can also be a scientific number with an "e" to
indicate the power of 10:
Example:
bool isCSharpFun = true;
bool isFishTasty = false;
Console.WriteLine(isCSharpFun); // Outputs True
Console.WriteLine(isFishTasty); // Outputs False
float f1 = 35e3F;
double d1 = 12E4D;
Console.WriteLine(f1);
Console.WriteLine(d1);
Boolean:A boolean data type is declared with
the bool keyword and can only take the values true
or false:
Boolean values are mostly used for conditional testing, which you will learn more about in a later chapter.
Characters
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c': char myGrade = 'B';
Console.WriteLine(myGrade);
Strings: The string data type is used to store a sequence of characters (text).
String values must be surrounded by double quotes:
Example :
string greeting = "Hello World";
Console.WriteLine(greeting);
C# Type Casting
Type casting is when you assign a value of one data type to another type.
In C#, there are two types of casting:
 Implicit Casting (automatically) - converting a smaller type to a larger type size
char -> int -> long -> float -> double
 Explicit Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char
Implicit Casting
Implicit casting is done automatically when passing a smaller size type
to a larger size type:
Example:
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
Console.WriteLine(myInt); // Outputs 9
Console.WriteLine(myDouble); // Outputs 9
Explicit Casting
Explicit casting must be done manually by placing the type in
parentheses in front of the value:
Example:
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
Console.WriteLine(myDouble); // Outputs 9.78
Console.WriteLine(myInt); // Outputs 9
Type Conversion Methods
It is also possible to convert data types explicitly by using built-in methods, such as
Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and
Convert.ToInt64 (long):
int myInt = 10;
double myDouble = 5.25;
bool myBool = true;
Console.WriteLine(Convert.ToString(myInt)); // convert int to string
Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double
Console.WriteLine(Convert.ToInt32(myDouble)); // convert double to int
Console.WriteLine(Convert.ToString(myBool)); // convert bool to
Why Conversion?
Many times, there's no need for type conversion. But sometimes you have to. Take a
look at the next chapter, when working with user input, to see an example of this
User Input
Get User Input
You have already learned that Console.WriteLine() is used to output (print) values. Now we will use
Console.ReadLine() to get user input.
In the following example, the user can input his or hers username, which is stored in the variable
userName. Then we print the value of userName:
// Type your username and press enter
Console.WriteLine("Enter username:");
// Create a string variable and get user input from the keyboard and store it in the variable
string userName = Console.ReadLine();
// Print the value of the variable (userName), which will display the input value
Console.WriteLine("Username is: " + userName);
User Input and Numbers
The Console.ReadLine() method returns a string. Therefore, you cannot get information from
another data type, such as int. The following program will cause an error:
 The error message will be something like this:
 Cannot implicitly convert type 'string' to 'int’
 Like the error message says, you cannot implicitly convert type 'string' to
'int'.
 Luckily, for you, you just learned from the previous chapter (Type
Casting), that you can convert any type explicitly, by using one of the
Convert.To methods:
Console.WriteLine("Enter your age:");
int age = Console.ReadLine();
Console.WriteLine("Your age is: " + age);
Console.WriteLine("Enter your age:");
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your age is: " + age);
Note: If you enter wrong input (e.g.
text in a numerical input), you will
get an exception/error message (like
System.FormatException:'Input
string was not in a correct format.').
END

data types in C-Sharp (C#)

  • 1.
  • 2.
    Data Types As explainedin the variables chapter, a variable in C# must be a specified by data type In C#, there are different types of variables (defined with different keywords), for example: int myNum = 5; // Integer (whole number) double myDoubleNum = 5.99D; // Floating point number char myLetter = 'D'; // Character bool myBool = true; // Boolean string myText = "Hello"; // String
  • 3.
    Why to useData Types…. A data type specifies the size and type of variable values. It is important to use the correct data type for the corresponding variable;  to avoid errors, to save time and memory it will also make your code more maintainable and readable.
  • 4.
    Common types ofData Types Data Type Size Description int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647 long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits bool 1 bit Stores true or false values char 2 bytes Stores a single character/letter, surrounded by single quotes string 2 bytes per character Stores a sequence of characters, surrounded by double quotes
  • 5.
    Numbers Number types aredivided into two groups: Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are int and long. Which type you should use, depends on the numeric value. Floating point types represents numbers with a fractional part, containing one or more decimals. Valid types are float and double.
  • 6.
    Integer Types Int: Theint data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value. Example : int myNum = 100000; Console.WriteLine(myNum); ◦ Long: The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L": Example : long myNum = 15000000000L; Console.WriteLine(myNum);
  • 7.
    Floating point types Float:The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "F": Example : float myNum = 5.75F; Console.WriteLine(myNum); ◦ Double: The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you can end the value with a "D" (although not required): Example : double myNum = 19.99D; Console.WriteLine(myNum); The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.
  • 8.
    Scientific Number A floatingpoint number can also be a scientific number with an "e" to indicate the power of 10: Example: bool isCSharpFun = true; bool isFishTasty = false; Console.WriteLine(isCSharpFun); // Outputs True Console.WriteLine(isFishTasty); // Outputs False float f1 = 35e3F; double d1 = 12E4D; Console.WriteLine(f1); Console.WriteLine(d1); Boolean:A boolean data type is declared with the bool keyword and can only take the values true or false: Boolean values are mostly used for conditional testing, which you will learn more about in a later chapter.
  • 9.
    Characters The char datatype is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c': char myGrade = 'B'; Console.WriteLine(myGrade); Strings: The string data type is used to store a sequence of characters (text). String values must be surrounded by double quotes: Example : string greeting = "Hello World"; Console.WriteLine(greeting);
  • 10.
    C# Type Casting Typecasting is when you assign a value of one data type to another type. In C#, there are two types of casting:  Implicit Casting (automatically) - converting a smaller type to a larger type size char -> int -> long -> float -> double  Explicit Casting (manually) - converting a larger type to a smaller size type double -> float -> long -> int -> char
  • 11.
    Implicit Casting Implicit castingis done automatically when passing a smaller size type to a larger size type: Example: int myInt = 9; double myDouble = myInt; // Automatic casting: int to double Console.WriteLine(myInt); // Outputs 9 Console.WriteLine(myDouble); // Outputs 9
  • 12.
    Explicit Casting Explicit castingmust be done manually by placing the type in parentheses in front of the value: Example: double myDouble = 9.78; int myInt = (int) myDouble; // Manual casting: double to int Console.WriteLine(myDouble); // Outputs 9.78 Console.WriteLine(myInt); // Outputs 9
  • 13.
    Type Conversion Methods Itis also possible to convert data types explicitly by using built-in methods, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long): int myInt = 10; double myDouble = 5.25; bool myBool = true; Console.WriteLine(Convert.ToString(myInt)); // convert int to string Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double Console.WriteLine(Convert.ToInt32(myDouble)); // convert double to int Console.WriteLine(Convert.ToString(myBool)); // convert bool to
  • 14.
    Why Conversion? Many times,there's no need for type conversion. But sometimes you have to. Take a look at the next chapter, when working with user input, to see an example of this
  • 15.
    User Input Get UserInput You have already learned that Console.WriteLine() is used to output (print) values. Now we will use Console.ReadLine() to get user input. In the following example, the user can input his or hers username, which is stored in the variable userName. Then we print the value of userName: // Type your username and press enter Console.WriteLine("Enter username:"); // Create a string variable and get user input from the keyboard and store it in the variable string userName = Console.ReadLine(); // Print the value of the variable (userName), which will display the input value Console.WriteLine("Username is: " + userName);
  • 16.
    User Input andNumbers The Console.ReadLine() method returns a string. Therefore, you cannot get information from another data type, such as int. The following program will cause an error:  The error message will be something like this:  Cannot implicitly convert type 'string' to 'int’  Like the error message says, you cannot implicitly convert type 'string' to 'int'.  Luckily, for you, you just learned from the previous chapter (Type Casting), that you can convert any type explicitly, by using one of the Convert.To methods: Console.WriteLine("Enter your age:"); int age = Console.ReadLine(); Console.WriteLine("Your age is: " + age); Console.WriteLine("Enter your age:"); int age = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Your age is: " + age); Note: If you enter wrong input (e.g. text in a numerical input), you will get an exception/error message (like System.FormatException:'Input string was not in a correct format.').
  • 17.