C Sharp Unit IV Notes
C Sharp Unit IV Notes
C Sharp Unit IV Notes
Operator overloading provides a flexible option for the creation of new definition for
most of the c# operators.
We can create a new language of our own by the creative use of the method and operator
overloading techniques.
Mathematical and physical modeling where we use classes to represent objects such as
co-ordinates, vectors, matrices, complex numbers and so on.
Graphical programs where co-ordinate related objects are used to represent positions on
the screen.
Text manipulations where classes are used to represent strings and sentences.
Overloadable Operators
The list of overloadable operators in c#
The return type can be of any type except void for unary operators like !, ~, + and dot (.) but the return
type must be the type of ‘Type’ for – and ++ operators and must be a bool type for true as well as false
operators. But do remember that the true and false operators can be overloaded as pairs only. The
compilation error arises if a class declares one of these operators without declaring the other.
class Calculator {
class EntryPoint
{
// Driver Code
static void Main(String []args)
{
// using overloaded - operator with the class object
Calculator calc = new Calculator(15, -25);
calc = -calc;
An overloaded binary operator must take two arguments; at least one of them must be of the type class or
struct, in which the operation is defined. But overloaded binary operators can return any value except the
type void. The general form of a overloaded binary operator is as follows.
public static return type operator op (Type1 t1, Type2 t2)
{
//Statements ;
}
Abstract classes to some extent serve the same purpose, however, they are mostly used when only few
methods are to be declared by the base class and the deriving class implements the functionalities.
Declaring Interfaces
Interfaces are declared using the interface keyword. It is similar to class declaration. Interface
statements are public by default. Following is an example of an interface declaration
public interface ITransactions {
// interface members
void showTransaction();
double getAmount();
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
namespace InterfaceApplication {
public Transaction() {
tCode = " ";
date = " ";
amount = 0.0;
}
public Transaction(string c, string d, double a) {
tCode = c;
date = d;
amount = a;
}
public double getAmount() {
return amount;
}
public void showTransaction() {
Console.WriteLine("Transaction: {0}", tCode);
Console.WriteLine("Date: {0}", date);
Console.WriteLine("Amount: {0}", getAmount());
}
}
class Tester {
t1.showTransaction();
t2.showTransaction();
Console.ReadKey();
}
Transaction: 001
Date: 8/10/2012
Amount: 78900
Transaction: 002
Date: 9/10/2012
Amount: 451900
Features
1. An abstract class can inherit from a class and one or more interfaces.
2. An abstract class contains abstract and non-Abstract methods.
3. An Abstract class can have modifiers for methods, properties etc.
4. An Abstract class can have constants and fields.
5. An abstract class can implement a property.
6. An abstract class can have constructors or destructors.
7. An abstract class cannot be inherited from by structures.
8. An abstract class cannot support multiple inheritance.
Abstract class and Interface Difference
try − A try block enclose the code that could throw an exception. A try
block identifies a block of code for which particular exceptions is
activated. It is followed by one or more catch blocks.
1 System.IO.IOException
Handles I/O errors.
2 System.IndexOutOfRangeException
3 System.ArrayTypeMismatchException
Handles errors generated when type is mismatched with the array type.
4 System.NullReferenceException
5 System.DivideByZeroException
6
System.InvalidCastException
Handles errors generated during typecasting.
System.OutOfMemoryException
7 Handles errors generated from insufficient free memory.
8
System.StackOverflowException
Handles errors generated from stack overflow.