Professional Documents
Culture Documents
Unit 2 PDF
Unit 2 PDF
Fundamental of C#.Net
Syntax, data types, variables, predefined data types, Flow
Control, Enum, Arrays, Preprocessor Directives, Classes,
Structures
Fundamental of OOPs
Inheritance
Polymorphism
Interface
Array
Events and Delegates
Variables and Types:
"Variables" are simply storage locations for data.
You can place data into them and retrieve their contents as
part of a C# expression.
The interpretation of the data in a variable is controlled
through "Types".
C# is a "Strongly Typed" language.
Thus all operations on variables are performed with
consideration of what the variable's "Type" is.
There are rules that define what operations are legal in
order to maintain the integrity of the data you put in a
variable.
The C# simple types consist of the Boolean type and three
numeric types - Integrals, Floating Point, Decimal, and String.
The term "Integrals", which is defined in the C#
Programming Language Specification, refers to the
classification of types that include :
sbyte, byte, short, ushort, int, uint, long, ulong, and char.
The Boolean Type:
Boolean types are declared using the keyword, bool. They
have two values: true or false.
In other languages, such as C and C++, boolean conditions
can be satisfied where 0 means false and anything else
means true.
using System;
class Booleans
{
public static void Main()
{
bool content = true;
bool noContent = false;
string name;
namespace InheritanceApplication {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle: Shape {
public int getArea() {
return (width * height);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
namespace InheritanceApplication {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle : Shape, PaintCost {
public int getArea() {
return (width * height);
}
public int getCost(int area) {
return area * 70;
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
namespace ProgramCall
{
class Class1
{
}
}
class MainClass
{
static void Main()
{
Console.WriteLine(obj.Sum(10, 20));
Console.WriteLine(obj.Sum(10, 15.70f));
Console.Read();
}
}
}
Method overloading provides more than one form for a
method.
Hence it is an example for polymorphism.
namespace methodoverriding
{
class BaseClass
{
public virtual string YourCity()
{
return "New York";
}
}
Output
London
Example - 2 implementing abstract method
using System;
namespace methodoverridingexample
{
abstract class BaseClass
{
public abstract string YourCity();
}
class DerivedClass : BaseClass
{
//It is mandatory to implement absract method
public override string YourCity()
{
return "London";
}
private int sum(int a, int b)
{
return a + b;
}
}
class Program
{
Output
London
Interface:
An interface looks like a class, but has no
implementation.
The only thing it contains are declarations of events,
indexers, methods and/or properties.
The reason interfaces only provide declarations is because
they are inherited by classes and structs, which must provide
an implementation for each interface member declared.
You can never instantiate an interface; it contains only the
signatures of its members.
An interface neither constructors nor fields
Interface definition is also not allowed to contain
operator overloads.
It is also not permitted to declare modifiers on the
members in an interface definition.
Interface are always implicitly “Public”, and cannot be
declared as “virtual” or “static”.
For example:
Public interface IDisposable
{
Void Dispose();
}
a = new int[5];
Jagged Array:
It is possible to have jagged array where rows may be different
size.
For this we need an array in which each element is another
array.
C# supports single dimensional, multi dimensional and
jagged array(array of array).
Examples:
int[] array a = new int[10];
// single dimensional array of int
for( int i =0; i < array)
array a[i] = i;
Try
{
//Statement
}
Catch(Exception ex)
{
//Step to handle error
}
Finally
{
//must be executed
}
The following step outline how this blocks to use to
handle error conditions:
1) The exception flow first entered into try block.
2) If no error occur in a try block execution proceed normally
through the block and when it reached end of try block
control will jump from try block to finally block. If error
occur within a try block, execution jump to a catch block.
3) The error condition is handled in the catch block.
4) At the end of catch block execution automatically transfer
to the finally block, if it is present.
5) The finally block is executed.
class EventProgram {
event MyDel MyEvent;
public EventProgram() {
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);
}
}
}
C# - Delegates
C# delegates are similar to pointers to functions, in C or C++.
A delegate is a reference type variable that holds the reference to a
method. The reference can be changed at runtime.
Delegates are especially used for implementing events and the call-
back methods. All delegates are implicitly derived from
the System.Delegate class.
Declaring Delegates
Delegate declaration determines the methods that can be referenced
by the delegate. A delegate can refer to a method, which has the same
signature as that of the delegate.
For example, consider a delegate −
public delegate int MyDelegate (string s);
The preceding delegate can be used to reference any method that has
a single string parameter and returns an int type variable.
Syntax for delegate declaration is −
delegate <return type> <delegate-name> <parameter list>
Instantiating Delegates
Once a delegate type is declared, a delegate object must
be created with the new keyword and be associated with
a particular method.
When creating a delegate, the argument passed to
the new expression is written similar to a method call,
but without the arguments to the method.
For example −
public delegate void printString(string s);
...
printString ps1 = new printString(WriteToScreen);
printString ps2 = new printString(WriteToFile);
Following example demonstrates declaration, instantiation,
and use of a delegate that can be used to reference methods
that take an integer parameter and returns an integer value.
using System;
delegate int NumberChanger(int n);
namespace DelegateAppl {
class TestDelegate {
static int num = 10;
public static int AddNum(int p) {
num += p;
return num;
}
public static int MultNum(int q) {
num *= q;
return num;
}
public static int getNum() {
return num;
}
static void Main(string[] args) {
//create delegate instances
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
Value of Num: 35
Value of Num: 175
Multicasting of a Delegate
Delegate objects can be composed using the "+" operator. A
composed delegate calls the two delegates it was composed
from. Only delegates of the same type can be composed.
The "-" operator can be used to remove a component
delegate from a composed delegate.
Using this property of delegates you can create an invocation
list of methods that will be called when a delegate is invoked.
This is called multicasting of a delegate.
The following program demonstrates multicasting of a
delegate −
using System;
nc = nc1;
nc += nc2;
//calling multicast
nc(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
Value of Num: 75
Assignment-2 - Visual Programming