Professional Documents
Culture Documents
Research Methology Lectures (Chapter 1 To 5)
Research Methology Lectures (Chapter 1 To 5)
What is C#?
C# is pronounced "C-Sharp".
It is an object-oriented programming language created by Microsoft that runs on the .NET Framework.
C# has roots from the C family, and the language is close to other popular languages like C++ and Java.
The first version was released in year 2002. The latest version, C# 8, was released in September 2019.
C# is used for:
Mobile applications
Desktop applications
Web applications
Web services
Web sites
Games
VR
Database applications
And much, much more!
Why Use C#?
using System;
namespace HelloWorld
{
class Program
{ static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
C# Variables
ariables are containers for storing data values.
In C#, there are different types of variables (defined with different keywords), for
example:
•int - stores integers (whole numbers), without decimals, such as 123 or -123
•double - stores floating point numbers, with decimals, such as 19.99 or -19.99
•char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single
quotes
•string - stores text, such as "Hello World". String values are surrounded by double
quotes
•bool - stores values with two states: true or false
Declaring (Creating) Variables
Syntax
type variableName = value;
Create a variable called name of type string and assign it the value
“Jama":
string name = “Jama"; Console.WriteLine(name);
Create a variable called myNum of type int and assign it the
value 15:
int myNum = 15; Console.WriteLine(myNum);
C# Data Types
Type casting is when you assign a value of one data type to another type.
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
// 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
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:
Example
Console.WriteLine("Enter your age:");
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your age is: " + age);
C# Operators
+= x += 3 x=x+3 Try
-= x -= 3 x=x-3 Try it
*= x *= 3 x=x*3 Try it »
/= x /= 3 x=x/3 Try it »
%= x %= 3 x=x%3 Try it »
|= x |= 3 x=x|3 Try it
^= x ^= 3 x=x^3 Try it
== Equal to x == y Try
&& Logical and Returns true if both statements are true x < 5 && x < 10 Try it »
! Logical not Reverse the result, returns false if the !(x < 5 && x < 10)
result is true
C# Exercises
Example
C# Switch Statements
Use the switch statement to select one of many code blocks to be
executed.
Syntax
switch(expression)
{
case x: // code block
break;
case y: // code block
break;
default: // code block break; }
Cont…
The default keyword is optional and specifies some code to run if there is no case
match:
Example
int day = 4;
switch (day)
{
case 6:
Console.WriteLine("Today is Saturday.");
break; case 7: Console.WriteLine("Today is Sunday.");
break;
default: Console.WriteLine("Looking forward to the Weekend.");
break;
} // Outputs "Looking forward to the Weekend."
Exercise
C# While Loop
The while loop loops through a block of code as long as a specified condition is True:
Syntax
while
(condition)
{
// code block to be executed
}
While loop
In the example below, the code in the loop will run, over and over again, as
long as a variable (i) is less than 5:
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
The Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute
the code block once, before checking if the condition is true, then it
will repeat the loop as long as the condition is true.
The example below uses a do/while loop. The loop will always be
executed at least once, even if the condition is false, because the
code block is executed before the condition is tested:
Example
int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i < 5);
C# For Loop
When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been
executed.
The example below will print the numbers 0 to 4
Example
The following example outputs all elements in the cars array, using
a foreach loop:
Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars)
{
Console.WriteLine(i);
}
C# Break and Continue
C# Break
You have already seen the break statement used in an earlier chapter of this
tutorial. It was used to "jump out" of a switch statement.
Create an Array
Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
To declare an array, define the variable type with square brackets:
string[] cars;
We have now declared a variable that holds an array of strings.
To insert values to it, we can use an array literal - place the values in a
comma-separated list, inside curly braces:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
To create an array of integers, you could write:
int[] myNum = {10, 20, 30, 40};
Access the Elements of an Array
o find out how many elements an array has, use the Length property:
Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars.Length);
// Outputs 4
Loop Through an Array
You can loop through the array elements with the for loop, and use the Length
property to specify how many times the loop should run.
The following example outputs all elements in the cars array:
Example
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.Length; i++)
{
Console.WriteLine(cars[i]);
}
C# Methods
Methods are used to perform certain actions, and they are also known as
functions.
Why use methods? To reuse code: define the code once, and use it many
times.
Create a Method
A method is defined with the name of the method, followed by parentheses ().
C# provides some pre-defined methods, which you already are familiar with, such
as Main(), but you can also create your own methods to perform certain actions:
Example
Create a method inside the Program class:
class Program
{
static void MyMethod()
{
// code to be executed
}
}
Cont..
Example Explained
MyMethod() is the name of the method
static means that the method belongs to the Program class and not an object
of the Program class. You will learn more about objects and how to access
methods through objects later in this tutorial.
void means that this method does not have a return value. You will learn
more about return values later in this chapter
Call a Method
To call (execute) a method, write the method's name followed by two parentheses () and a
semicolon;
In the following example, MyMethod() is used to print a text (the action), when it is called:
Example
Inside Main(), call the myMethod() method:
namespace Loop_Examples1
{
class Program
{
static void Main(string[] args)
{
int num, i, result;
Console.Write("Enter a number\t");
num = Convert.ToInt32(Console.ReadLine());
namespace Loop_Example2
{
class Program
{
static void Main(string[] args)
{
int i, j;
i = 0;
j = 0;
They are specified after the method name, inside the parentheses. You can
add as many parameters as you want, just separate them with a comma.
The following example has a method that takes a string called fname as
parameter. When the method is called, we pass along a first name, which is
used inside the method to print the full name:
Example
static void MyMethod(string fname)
{
Console.WriteLine(fname + " Warsame");
}
// Sweden
// India
// Somalia
Multiple Parameters
static void MyMethod(string fname, int age)
{
Console.WriteLine(fname + " is " + age);
}
static void Main(string[] args)
{
MyMethod(“Sulayman", 5);
MyMethod(“Said", 8);
MyMethod(“Najax", 31);
}
Note that when you are working with multiple parameters, the method call
must have the same number of arguments as there are parameters, and the
arguments must be passed in the same order.
Return Values
The void keyword, used in the examples above, indicates that the method should not
return a value. If you want the method to return a value, you can use a primitive
data type (such as int or double) instead of void, and use the return keyword inside
the method:
Example
static int MyMethod(int x)
{
return 5 + x;
}
// Outputs 8 (5 + 3)
Example
// Outputs 8 (5 + 3)
Example 2
static int MyMethod(int x, int y)
{
return x + y;
}
// Outputs 8 (5 + 3)
C# Method Overloading
With method overloading, multiple methods can have the same name with
different parameters:
Example
int MyMethod(int x)
float MyMethod(float x)
double MyMethod(double x, double y)
Consider the following example, which have two methods that add numbers
of different type:
static int PlusMethodInt(int x, int y)
{
return x + y;
}
Procedural programming is about writing procedures or methods that perform operations on the
data, while object-oriented programming is about creating objects that contain both data and
methods.
Look at the following illustration to see the difference between class and objects:
class
Fruit
objects
Apple
Banana
Mango
So, a class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the variables and
methods from the class.
Classes and Objects
You learned from the previous chapter that C# is an object-oriented
programming language.
/ The class
class MyClass
{
// Class members
string color = "red"; // field
int maxSpeed = 200; // field
public void fullThrottle() // method
{
Console.WriteLine("The car is going as fast as it can!");
}
}
C# Constructors
Constructors
A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created. It
can be used to set initial values for fields:
Example
class Car
{
public string model; // Create a field
// Create a class constructor for the Car class
public Car()
{
model = "Mustang"; // Set the initial value for model
}
static void Main(string[] args)
{
Car Ford = new Car(); // Create an object of the Car Class (this will call the constructor)
Console.WriteLine(Ford.model); // Print the value of model
}}
// Outputs "Mustang"
Cont…
Note that the constructor name must match the class name, and it cannot
have a return type (like void or int).
Also note that the constructor is called when the object is created.
Constructors save time! Take a look at the last example on this page to really
understand why.
Constructor Parameters
// Outputs "Mustang"
You can have as many parameters as you want:
class Car
{
public string model;
public string color;
public int year;
Modifier Description
public The code is accessible for all classes
private The code is only accessible within the same class
protected The code is accessible within the same class, or in a class that is
inherited from that class. You will learn more about inheritance
in a later chapter
internal The code is only accessible within its own assembly, but not from
another assembly. You will learn more about this in a later
chapter
Private Modifier
If you declare a field with a private access modifier, it can only be accessed
within the same class:
Example.
class Car
{
private string model;
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.model);
}
}
Public Modifier
If you declare a field with a public access modifier, it is accessible for all classes:
class Car
{
public string model = "Mustang";
}
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.model);
}
}
Cont…
Why Access Modifiers?
To control the visibility of class members (the security level of each individual
class and class member).
To achieve "Encapsulation" - which is the process of making sure that
"sensitive" data is hidden from users. This is done by declaring fields as
private. You will learn more about this in the next chapter.
Note: By default, all members of a class are private if you don't specify an
access modifier:
C# Properties (Get and Set)
Properties and Encapsulation
Before we start to explain properties, you should have a basic understanding
of "Encapsulation".
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden
from users. To achieve this, you must:
declare fields/variables as private
provide public get and set methods, through properties, to access and update
the value of a private field
Properties
You learned from the previous chapter that private variables can only be accessed within the same
class (an outside class has no access to it). However, sometimes we need to access them - and it can
be done with properties.
A property is like a combination of a variable and a method, and it has two methods: a get and a set
method:
Example
class Person
{
private string name; // field
class Program
{
static void Main(string[] args)
{
Person myObj = new Person();
myObj.Name = "Liam";
Console.WriteLine(myObj.Name);
}
Cont..
Why Encapsulation?
Better control of class members (reduce the possibility of yourself (or others)
to mess up the code)
Fields can be made read-only (if you only use the get method), or write-only
(if you only use the set method)
Flexible: the programmer can change one part of the code without affecting
other parts
Increased security of data
C# Inheritance
Inheritance (Derived and Base Class)
In C#, it is possible to inherit fields and methods from one class to another.
We group the "inheritance concept" into two categories:
Derived Class (child) - the class that inherits from another class
Base Class (parent) - the class being inherited from
To inherit from a class, use the : symbol.
In the example below, the Car class (child) inherits the fields and methods
from the Vehicle class (parent):
class Vehicle // base class (parent)
{
public string brand = "Ford"; // Vehicle field
public void honk() // Vehicle method
{
Console.WriteLine("Tuut, tuut!");
} }
class Car : Vehicle // derived class (child)
{
public string modelName = "Mustang"; // Car field
}
class Program
{
static void Main(string[] args)
{
// Create a myCar object
Car myCar = new Car();
// Call the honk() method (From the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand field (from the Vehicle class) and the value of the modelName from the Car class
Console.WriteLine(myCar.brand + " " + myCar.modelName);
}}
The sealed Keyword
if you don't want other classes to inherit from a class, use the sealed
keyword:
If you try to access a sealed class, C# will generate an error:
sealed class Vehicle
{
...
}
To create an enum, use the enum keyword (instead of class or interface), and
separate the enum items with a comma:
enum Level
{
Low,
Medium,
High
}
You can access enum items with the dot syntax:
Level myVar = Level.Medium;
Console.WriteLine(myVar);