C Sharp ( C#)
Ahmad Damra
MCT, MCSD, MCPD, MCTs
FB.COM/Mr.AhmadDamra
©2017 Ahmad Damra
String type
1. We will learn the basic structure of a c# program. The program we used
in this video is shown below.
©2017 Ahmad Damra
String type
©2017 Ahmad Damra
Built-in types
©2017 Ahmad Damra
common operators
©2017 Ahmad Damra
common operators
©2017 Ahmad Damra
Data type conversions
©2017 Ahmad Damra
Data type conversions
©2017 Ahmad Damra
Data type conversions
©2017 Ahmad Damra
Comments
©2017 Ahmad Damra
If Statement
©2017 Ahmad Damra
If Statement
Exampl 3
int x = 20; int y=50;
if(x==2 || y==20)
{
Label1.Text = "Welcome";
}
else
{
Label1.Text = "No";
}
©2017 Ahmad Damra
switch statement
©2017 Ahmad Damra
Loops
©2017 Ahmad Damra
Loop : For
Example 1 : Even Nume
int i = 0;
for (i = 0; i < 20;i+=2)
{
Label1.Text += num[i] + " ";
}
©2017 Ahmad Damra
Loop foreach
©2017 Ahmad Damra
Methods
access-modifier return-type method-name parameter
©2017 Ahmad Damra
Method
©2017 Ahmad Damra
Namespace
namespace ProjectA
{
namespace TeamA
{
class ClassA
{
public static void print()
{
}
}}}
Call namespace : using ProjectA.TeamA
If namespace to long :
©2017 Ahmad Damra
Introduction to classes
©2017 Ahmad Damra
What is a class?
Call Class :
customer c1 = new customer("Ahmad", "damra");
TextBox1.Text = c1.PrintFullName();
©2017 Ahmad Damra
Inheritance
Why Inheritance
Advantages of inheritance
Inheritance syntax
Inheritance concepts
©2017 Ahmad Damra
No Inheritance
©2017 Ahmad Damra
Using inheritance
Move all the common code into base Employee class
FullTIme and PartTime employee specific code in the respective derived class
©2017 Ahmad Damra
Inheritance Example
public class Employee { public class FullTimeEmp:Employee
public string FirstName, LastName, Email; {
public float YearlySalary;
}
public string PrintFullName() { public class PartTimeEmp : Employee {
string n; public float HourlyRate;
}
n = FirstName+ “ " + LastName ;
Call Method:
return n; FullTimeEmp FTE = new FullTimeEmp();
} FTE.FirstName = "ahmad";
FTE.LastName = "Damra";
}
TextBox1.Text= FTE.PrintFullName();
©2017 Ahmad Damra
Why Inheritance
1. Inheritance is one of the primary object oriented programming
2. It allow code reuse
3. Code reuse can reduce time and errors
Note: you will specify all the common fields, properties, methods in the base
class, which allows reusability. In the derived class you will only have fields,
properties and methods that are specific to them.
©2017 Ahmad Damra
Polymorphism
Employee1 Base Class
public class Employee1
{
public string FirstName = "Ahmad",
LastName = "Damra",
Email = "Ahamd@IT.com";
public string n;
public virtual string PrintFullName()
{
n = FirstName + " " + LastName;
return n;
}
}
©2017 Ahmad Damra
Polymorphism: Override Class
public class FullTimeEmp : Employee1{
public override string PrintFullName() {
n = FirstName + " " + LastName + " - Full Time";
return n;
} }
public class PartTimeEmp : Employee1
{ public override string PrintFullName()
{ n = FirstName + " " + LastName + " - Part Time";
return n;
}}
©2017 Ahmad Damra
Polymorphism: invoke Class
protected void Button1_Click(object sender, EventArgs e)
{
Employee1[] emploees = new Employee1[4];
emploees[0] = new Employee1();
emploees[1] = new PartTimeEmp();
emploees[2] = new FullTimeEmp();
foreach (Employee1 r in emploees)
{
string x1 = r.PrintFullName() ;
Label1.Text = Label1.Text + "<br />" +
x1.ToString();
}
}
©2017 Ahmad Damra
Exception Handling
An exception is an unforeseen error that occurs when a program is running.
Example:
Trying to read from a file that does not exist, throws FileNotFoundException.
Trying to read from a database table that does not exist, throws a
SqlException
Showing actual unhandled exceptions to the end user is bad for two
reasons:
An exception is actually a class that derives from System. Exception class.
The System. Exception class has several useful properties, that provide
valuable information about the exception.
Message: Gets a message that describes the current exception
Stack Track: Provides the call stack to the line number in the method where
the exception occurred.
©2017 Ahmad Damra
Inner Exception Example
try {
int n1 = Convert.ToInt16( TextBox1.Text);
int n2 = Convert.ToInt16(TextBox2.Text);
int result = n1 / n2;
Label1.Text = result.ToString();
}
catch (Exception ex) {
ex.Message;
}
©2017 Ahmad Damra