C# Unit-5
C# Unit-5
Interfaces
EXAMPLE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceApplication
{
publicinterfaceITransaction
{
void showTransaction();
double getAmount();
Transaction(string c, string d, double a);
}
publicclassTransaction : ITransaction
{
privatestring tcode;
privatestring date;
privatedouble amount;
public Transaction(string c, string d, double a)
{
tcode = c;
date = d;
amount = a;
}
publicdouble getAmount()
1
{
return amount;
}
publicvoid showTransaction()
{
Console.WriteLine("Transaction: {0}",tcode);
Console.WriteLine("Date: {0}", date);
Console.WriteLine("Amount: {0}", getAmount());
}
}
classTester
{
staticvoid Main(string[] args)
{
Transaction t1 =
newTransaction("001","8/2/2019",78900.00);
Transaction t2 = new Transaction ("002", "9/2/2019" ,
451900.00);
t1.showTransaction();
t2.showTransaction();
Console.ReadKey();
}
}
}
Operator Overloading
EXAMPLE
2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace overloading
{
classCalculate
{
publicint n1, n2;
public Calculate(int no1, int no2)
{
n1 = no1;
n2 = no2;
}
3
}
}
}
OUTPUT
Number 1:20
Number 2: -40
Delegates
Declaring Delegates
delegate<return type><delegate-name><parameter
list>
Instantiating Delegates
4
EXAMPLE
Multicasting of a Delegate
EXAMPLE
namespaceDelegateAppl
{
classTestDelegate
{
staticintnum = 10;
public static intAddNum(int p)
{
num += p;
returnnum;
5
}
public static intMultNum(int q)
{
num *= q;
returnnum;
}
public static intgetNum()
{
returnnum;
}
static void Main(string[] args)
{
//create delegate instances
NumberChangernc;
NumberChanger nc1 = newNumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
nc = nc1;
nc += nc2;
//calling multicast
nc(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
Result
Value of Num: 75
6
Using Delegates
Events
Declaring Events
syntax
– event MyDelMyEvent;
7
public delegate string MyDel(string str);
EXAMPLE
classEventProgram
{
eventMyDelMyEvent;
publicEventProgram()
{
this.MyEvent += new MyDel(this.WelcomeUser);
}
public string WelcomeUser(string username)
{
return "Welcome " + username;
}
static void Main(string[] args)
{
EventProgram obj1 = new EventProgram();
string result = obj1.MyEvent("Tutorials Point");
Console.WriteLine(result);
} }}
Output
Exception Handling
8
• C# exception handling is built upon four
keywords: try, catch, finally, and throw.
Syntax
try
{
// statements causing exception
}
catch(ExceptionName e1 )
{
// error handling code
9
}
catch(ExceptionName e2 )
{
// error handling code
}
catch(ExceptionNameeN )
{
// error handling code
}
Finally
{
// statements to be executed
}
Nested try
try
{
//do something
Try
{
//do something
if exception is thrown, don't go to parent catch
}
catch(Exception ex) {...} }
catch(Exception ex) { .... }
Exception Classes in C#
10
• The System.ApplicationException class supports
exceptions generated by application programs
11
System.OutOfMemoryException Handles errors generated
from insufficient free
memory.
Handling Exceptions
classDivNumbers
{
int result;
DivNumbers()
{
result = 0;
}
public void division(int num1, int num2)
{
try
{
result = num1 / num2;
}
catch (DivideByZeroException e)
12
{
Console.WriteLine("Exception caught: {0}", e);
}
Finally
{
Console.WriteLine("Result: {0}", result);
}
}
static void Main(string[] args)
{
DivNumbers d = new DivNumbers();
d.division(25, 0);
Console.ReadKey();
}
}
Throwing Objects
Classification of Errors
13
Logic Errors
Run-time Errors
14
impossible to carry out. You can fix most run-time
errors by rewriting the faulty code, and then
recompiling and running it.
Syntax Errors
15
Checked and Unchecked Operator
Checked Operator:
Unchecked Operator:
EXAMPLE
try
{
int a = 200000;
int b = 300000;
int c = checked(a * b);
Console.WriteLine(c.ToString());
}
catch(Exception ex)
{
Console.WriteLine("Your multiplication cross the limit"
);
}
16
Console.ReadKey();
try
{
int a = 2000000;
int b = 3000000;
int c =unchecked(a * b);
Console.WriteLine(c.ToString());
}
catch(Exception ex)
{
Console.WriteLine("Your multiplication cross the limit"
);
}
Console.ReadKey();
}
17