C Programming Language Day 2
C Programming Language Day 2
C Programming Language Day 2
Language
M R . J O H N PA U L B . V I L L A R I N
C# 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, but it will also make your code more maintainable and readable. The
most common data types are:
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.
Floating point types represents numbers with a fractional part, containing one or more decimals. Valid types are float and
double
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.
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":
Float Example:
float myNum = 5.75F;
Console.WriteLine(myNum);
Double Example:
Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate the power of 10:
float f1 = 35e3F;
double d1 = 12e4D;
Console.WriteLine(f1);
Console.WriteLine(d1);
Booleans
A boolean data type is declared with the bool keyword and can only take the values true or false;
Example:
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);
Console.WriteLine(myDouble);
Explicit Casting
Explicit casting must be done manually by placing the type in parentheses in front of the value:
Example:
Console.WriteLine(myDouble);
Console.WriteLine(myInt);
Example:
Console.WriteLine(“Enter userName”);
string userName = Console.ReadLine();
Console.WriteLine(“Username is: ” +userName);
C# 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.
Example:
Console.WriteLine(“Enter userName”);
string userName = Console.ReadLine();
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:
Console.WriteLine(“Enter your Age:”);
int age= Console.ReadLine();
Console.WriteLine(“Your age is: ” +age);
Output: Cannot implicitly convert type 'string' to 'int’
Output:
True
C# Comparison Operators
• A list of all comparison operators:
C# Comparison Operators
• A list of all comparison operators:
C# Logical Operators
• Logical operators are used to determine the logic between variables or values:
Example:
int x = 5;
Console.WriteLine(x > 3 && x < 10);
C# Logical Operators
• Logical operators are used to determine the logic between variables or values:
Example:
int x = 5;
Console.WriteLine(x > 3 && x < 10);
C# Math
The C# Math class has many methods that allows you to perform mathematical tasks on numbers.
Math.Max(x,y)- method can be used to find the highest value of x and y:
Console.WriteLine(Math.Max(5, 10));
Math.Min(x,y)- method can be used to find the lowest value of of x and y:
Console.WriteLine(Math.Min(5, 10));
Math.Sqrt(x)- method returns the square root of x:
Console.WriteLine(Math.Sqrt(64));
Math.Abs(x)-method returns the absolute (positive) value of x:
Console.WriteLine(Math.Abs(-4.7));
Math.Round()-rounds a number to the nearest whole number:
Console.WriteLine(Math.Round(9.99));
C# Strings
Strings are used for storing text.
String Length- A string in C# is actually an object, which contain properties and methods that can perform certain
operations on strings. For example, the length of a string can be found with the Length property.
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Console.WriteLine("The length of the txt string is: " + txt.Length);
Other Methods
There are many string methods available, for example ToUpper() and ToLower() which returns a
copy of the string converted to uppercase or lowercase:
string txt = "Hello World";
Console.WriteLine(txt.ToUpper()); // Outputs "HELLO WORLD"
Console.WriteLine(txt.ToLower()); //Outputs “hello world”
String Concatenation
The + operator can be used between strings to combine them. This is called concatenation:
string firstName = "John ";
string lastName = "Doe";
string name = firstName + lastName;
Console.WriteLine(name);
You can also use string.Concat() the method to concatenate two strings:
Console.WriteLine(name);
String Interpolation
Another option of string concatenation, is string interpolation, which substitutes values of variables into
placeholders in a string. Note that you do not have to worry about spaces, like with concatenation:
string firstName = "John";
Console.WriteLine(name);
Access Strings
You can access the characters in a string by referring to its index number inside square brackets [].
string myString = "Hello";
Console.WriteLine(myString[0]); Output:
H
String Interpolation
You can also find the index position of a specific character in a string, by using the
IndexOf() method:
string myString = "Hello";
Console.WriteLine(myString.IndexOf("e"));
Output:
1
Thank you
M R . J O H N PA U L B . V I L L A R I N