c Sharp Part i Mahedeev1 130718012115 Phpapp01

114
Md. Mahedee Hasan Senior Software Engineer LEADS Corporation Limited

Transcript of c Sharp Part i Mahedeev1 130718012115 Phpapp01

Page 1: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 1/114

Md. Mahedee Hasan

Senior Software Engineer

LEADS Corporation Limited

Page 2: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 2/114

Introduction

C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application Conditional & Iteration Statement

Class, Method & Constructor

Static Class Member

Designing Object

Inheritance Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 3: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 3/114

Introduction C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application

Conditional & Iteration Statement

Class, Method & Constructor

XML Comments

Static Class Member

Designing Object

Inheritance

Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 4: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 4/114

C# is intended to be a simple, modern, general-purpose, object-oriented programminglanguage

It is very sophisticated programming language It is developed by Microsoft with its .NET

initiatives

The language is intended for use in developingSoftware Components

Page 5: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 5/114

Introduction

C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application Conditional & Iteration Statement

Class, Method & Constructor

XML Comments

Static Class Member

Designing Object Inheritance

Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 6: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 6/114

Program execution begins at Main()

The using keyword refers to resources in the.NET framework class library

Statements are commands that perform actions A program is made up of many separate statement

Statement are separated by a semicolon

Braces are used to group statement

Page 7: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 7/114

Use indentation to indicate enclosingstatements

C# is case sensitive

White space is ignored

Indicate single line comment by using //

Indicate multiple line comment by using

/*and*/

Page 8: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 8/114

Introduction C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application

Conditional & Iteration Statement

Class, Method & Constructor

XML Comments

Static Class Member

Designing Object

Inheritance

Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 9: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 9/114

Types are used to declare variables

Variables store different kinds of data

Predefined types are those provided by C# and

.NET framework You can also defined your own

Variable must be declared before you can use

them

Page 10: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 10/114

Page 11: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 11/114

A variable is a storage location for a particular type Declaring

Assign a type

Assign a name

End with a semicolon

Ex. int noOfUser; string firstName;

Initializing Use assignment operator

Assign a value

End with a semicolon Ex. string firstName = “Mahedee”; 

Assigning literal variable Add a type suffix

Ex. decimal deposit = 50000M;

Page 12: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 12/114

string str = “Hello world”; //Hello world 

Literal string

string str = “\”Hello\””; //”Hello” 

Escape character string str = “Hello\nWorld”; \\ a new line is added

between Hello and World

Using verbatim string string str = @”Hello\n”; \\Hello\n

Page 13: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 13/114

Declare using const keyword and type

You must assign a value at the time ofdeclaration

Examples const double pi = 3.14;

const int earthRadius = 6378;

Page 14: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 14/114

User defined data type

Purpose of enumeration type is to use constantvalues

Process to create enumeration type Create an enumeration type

Declare variable of that type

Assign values to those variables

Page 15: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 15/114

Defining enumeration typesenum BluechipTeam{

Azad,

Mahedee,

Sarwar, Jamil

}

Using enumeration typesBluechipTeam aMember = BluechipTeam.Mahedee;

Displaying the variablesConsole.WriteLine(aMember); 

Page 16: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 16/114

Page 17: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 17/114

Introduction

C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application

Conditional & Iteration Statement Class, Method & Constructor

XML Comments

Static Class Member

Designing Object

Inheritance Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 18: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 18/114

An expression is a sequence of operators andoperands

The purpose of writing an expression is to

perform an action and return a value

Page 19: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 19/114

Expressions are evaluated according to operatorprecedence Example: 10 + 20 / 5 (result is 14)

Parenthesis can be used to control the order ofevaluation. Ex. (10 + 20) / 5 (result is 6)

Operator precedence is also determined byassociativity Binary operators are left associative i.e evaluated from

left to right.

Assignment and conditional operators are rightassociative i.e evaluated from right to left

Page 20: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 20/114

Introduction C# Fundamentals C# Pre defined Types C# Expressions Debugging Application Conditional & Iteration Statement Class, Method & Constructor XML Comments Static Class Member Designing Object

Inheritance Polymorphism Arrays & Collections Interface Exception Handling

Page 21: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 21/114

Page 22: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 22/114

Step In = F11 = Step into a Method

Step Out = Shift + F11 = Steps up and out of amethod back to the caller

Step Over = F10 = Steps past a method to thenext statement

Stop Debugging = Shift + F5 = Stops a

debugging session

Page 23: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 23/114

Page 24: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 24/114

Page 25: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 25/114

A conditional statement allows you to controlthe flow of your application by selecting thestatement that is executed, based on the value

of a Boolean expression.

Page 26: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 26/114

if statementif ( sales > 10000 ) {

bonus += .05 * sales;}

if else statementif ( sales > 10000 ) {

bonus += .05 * sales;}else {

bonus = 0;}

if else if satementif ( sales > 10000 ) {

bonus += .05 * sales;}else if ( sales > 5000 ) {

bonus = .01 * sales;}else {

bonus = 0;if ( priorBonus == 0 ) {// Schedule a Meeting;}

}

Page 27: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 27/114

Switch statements are useful for selecting one branchof execution from a list of mutually-exclusive choices.

switch( favoriteAnimal ) {

case Animal.Antelope:// herbivore-specific statements

break;

case Animal.Elephant:

// herbivore-specific statements

break;

case Animal.Lion:

// carnivore-specific statements

break;case Animal.Osprey:

// carnivore-specific statements

break;

default:

//default statemennt

}

Page 28: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 28/114

Page 29: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 29/114

Use when you know how many times youwant to repeat the execution of the code

Syntax:

for (initializer; condition; iterator) {statement-block

}

Example:for ( int i = 0; i < 10; i++ ) {

Console.WriteLine( "i = {0}",i );

}

Page 30: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 30/114

A Boolean test runs at the start of the loop andif tests as False, the loop is never executed.

The loop executed until the condition becomes

false. Syntax:

while (true-condition) {

statement-block

}

Examplewhile ( i <= 10 ) {

Console.WriteLine(i++);

}

Page 31: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 31/114

The continue keyword to start the next loop iterationwithout executing any remaining statements

The break keyword is encountered, the loop isterminated

Example:int i = 0;

while ( true ) {

i++;

if(i>5)

continue;if(i>= 10)

break;

Console.WriteLine(i);

}

Page 32: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 32/114

Executes the code in the loop and thenperforms a Boolean test. If the expression astrue then the loop repeats until the expressiontest as false

Syntax:do {

statements

} while (boolean-expression);

Example:int i = 1;

do {

Console.WriteLine("{0}", i++);

} while ( i <= 10 );

Page 33: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 33/114

Introduction C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application

Conditional & Iteration Statement Class, Method & Constructor

XML Comments

Static Class Member

Designing Object

Inheritance Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 34: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 34/114

Page 35: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 35/114

Value Types

Directly contain data

Stored on the stack

Must be initialized Cannot be null

An int is a value type

Example: int a; a = 15;

a15

Page 36: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 36/114

Contain a reference to the data

Stored on the heap

Declared using new key word

.NET garbage collection handles destruction

A class is a reference type

Example: EmployeeInfo c;

c* 15

Page 37: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 37/114

Boxing

Treat value types like reference types

Example: object boxedValue = (object) x;

Unboxing Treat reference types like value types

Example: int y = (int) boxedValue;

Page 38: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 38/114

How to define classpublic class Investor

{

public string firstName;

public string lastName;

public double purchasePower;

}

How to create an object Example: Investor objInvestor = new Investor();

How to access class variable Example: objInvestor.firstName = “Mahedee”; 

Page 39: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 39/114

Declaring namespacenamespace Bluchip{

public class Investor{

public string firstName;public string lastName;public double purchasePower;

}

}

Nested namespacesnamespace Bluchip{namespace InvestorSetup{

public class Investor{

//to do}

}}

The using namespace using Bluechip using Bluechip.InvestorSetup

Page 40: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 40/114

Access modifiers are used to define theaccessibility level of class members

public: Access not limited

private: Access limited to the containing class internal: Access limited to this program

protected: Access limited to the containing class andto types derived from the containing class

protected internal: Access limited to the containingclass, derived classes or to members of this program.

Page 41: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 41/114

A method is a class member that is used to define the actions Declare Mathod:

class Lion {

private int weight;

public bool IsNormalWeight() {

if ( ( weight < 100 ) || ( weight > 250 ) ) {

return false;}

return true;

}

public void Eat() { }

public int GetWeight() {

return weight;

}

}

Invoke method:Lion bigLion = new Lion();

if ( bigLion.IsNormalWeight() == false ) {

Console.WriteLine("Lion weight is abnormal");

}

Page 42: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 42/114

Passing by valueclass Lion {

private int weight;

public void SetWeight( int newWeight ) {

weight = newWeight;}

}

Lion bigLion = new Lion();

int bigLionWeight = 200;bigLion.SetWeight( bigLionWeight );

Page 43: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 43/114

Passing by reference

Using the ref keyword

Definite assignment

Using out parameter keyword Allow you to initialize variable in method

Use if you want a method to modify or returnmultiple values

Achieve this by passing the method a reference 

Page 44: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 44/114

class Zoo {

private int streetNumber = 123;

private string streetName = "High Street";

private string cityName = "Sammamish";

public void GetAddress(ref int number, ref string street, ref stringcity)

{

number = streetNumber;

street = streetName;

city = cityName;

}

}

Page 45: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 45/114

class ClassMain {

static void Main(string[] args) {

Zoo localZoo = new Zoo();

// note these variables are not initialized

int zooStreetNumber;

string zooStreetName;

string zooCity;

localZoo.GetAddress(out zooStreetNumber, out zooStreetName, out zooCity);

Console.WriteLine(zooCity);

// Writes "Sammamish" to a console

}

}

Page 46: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 46/114

When you pass a reference type to a method, themethod can alter the actual object.

using System;namespace LearningCSharp {

class MainClass {static void Main(string[] args) {Zoo myZoo = new Zoo();

Lion babyLion = new Lion();myZoo.AddLion( babyLion );}

}

class Lion {public string location;

}

class Zoo {public void AddLion( Lion newLion ) {newLion.location = "Exhibit 3";}

}}

Page 47: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 47/114

Method overloading is a language feature thatenables you to create multiple methods in oneclass that have the same name but that take

different signatures By overloading a method, you provide the

users of your class with a consistent name foran action while also providing them with

several ways to apply that action. Overloaded methods are a good way for you to

add new functionality to existing code.

Page 48: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 48/114

class Zoo {

public void AddLion( Lion newLion ) {

// Place lion in an appropriate exhibit

}

public void AddLion( Lion newLion, int exhibitNumber ) {

// Place the lion in exhibitNumber exhibit

}

}

Zoo myZoo = new Zoo();Lion babyLion = new Lion();

myZoo.AddLion( babyLion );

myZoo.AddLion( babyLion, 2 );

Page 49: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 49/114

Constructors are special methods thatimplement the actions that are required toinitialize an object.

Instance constructors are special type methodsthat implements the actions required toinitialize an object.

Have the same name as the name of the class

Default constructor takes no parameter

Page 50: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 50/114

public class Lion {

private string name;

public Lion() {

Console.WriteLine("Constructing Lion");

}

public Lion( string newLionName ) {this.name = newLionName;

}

}

Lion babyLion = new Lion();

Console.WriteLine("Made a new Lion object");

Output:

Constructing Lion

Made a new Lion object

Page 51: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 51/114

When you use the readonly modifier on a membervariable, you can only assign it a value when the classor object initializes, either by directly assigning themember variable a value, or by assigning it in the

constructor. Use the readonly modifier when a const keyword is not

appropriate because you are not using a literalvalue—meaning that the actual value of the variable is

not known at the time of compilation.

Page 52: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 52/114

class Zoo {

private int numberAnimals;

public readonly decimal admissionPrice;

public Zoo() {

// Get the numberAnimals from some source...

if ( numberAnimals > 50 ) {admissionPrice = 25;

}

else {

admissionPrice = 20;

}}

}

Page 53: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 53/114

Create multiple constructor that have samename but different signatures

It is often useful to overload a constructor to

allow instances to be created in more than oneway.

Page 54: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 54/114

Page 55: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 55/114

Introduction

C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application

Conditional & Iteration Statement

Class, Method & Constructor

XML comments

Static Class Member

Designing Object

Inheritance

Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 56: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 56/114

Page 57: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 57/114

Page 58: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 58/114

Page 59: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 59/114

Static members belong to the class, rather than aninstance.

Static constructors are used to initialize a class.

Initialize before an instance of the class is created.

Shared by all instance of the class

Classes can have static members, such as properties,methods and variables.

Because static members belong to the class, rather thanan instance, they are accessed through the class, notthrough an instance of the class.

Page 60: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 60/114

using System;

namespace StaticExample {

class ZooDemo {

static void Main(string[] args) {

Console.WriteLine( "Family: {0}", Lion.family );

Console.ReadLine();}

}

class Lion {

public static string family = "felidae";

}

}

Output: fedilae

Page 61: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 61/114

Instance constructors are used to initialize anobject

Static constructors are used to initialize a class

Will only ever be executed once Run before the first object of that type is

created.

Have no parameter Do not take an access modifier

May co-exist with a class constructor

Page 62: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 62/114

using System;namespace StaticConstructor {

class RandomNumberGenerator {

private static Random randomNumber;

static RandomNumberGenerator() {

randomNumber = new Random();

}

public int Next() {return randomNumber.Next();

}

}

class Class1 {

static void Main(string[] args) {

RandomNumberGenerator r = new RandomNumberGenerator();

for ( int i = 0; i < 10; i++ ) {

Console.WriteLine( r.Next() );

}

}

}

}

Page 63: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 63/114

Introduction C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application

Conditional & Iteration Statement Class, Method & Constructor

XML Comments

Static Class Member

Designing Object

Inheritance Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 64: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 64/114

Structure Design Object Oriented Design

Process centered Object centered

Reveals data Hide data

Single unit Modular unit

One time use ReusableOrdered algorithm No ordered algorithm

Page 65: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 65/114

Programs are easier to design because objectsreflect real-world items.

Applications are easier for users because data -

they do not need is hidden. Objects are self-contained units.

Productivity increases because you can reusecode.

Systems are easier to maintain and adapt tochanging business needs.

Page 66: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 66/114

Grouping related piece of information andprocesses into self-contained unit.

Makes it easy to change the way things work underthe cover without changing the way users interact.

Hiding internal details.

Makes your object easy to use.

Page 67: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 67/114

Protect access to the state of object.

 It like fields, but they operate much likemethods.

The get and set statements are called accessors.private double balance;

public double Balance {

get {

return balance;

}set {

balance = value;

}

}

Page 68: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 68/114

Introduction C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application

Conditional & Iteration Statement Class, Method & Constructor

XML Comments

Static Class Member

Designing Object

Inheritance Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 69: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 69/114

Inheritance specifies an is-a kind ofrelationship

Derived classes inherits properties and

methods from base class, allowing code reuse Derived classes become more specialized.

Page 70: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 70/114

public class Animal {public bool IsSleeping;

public void Sleep() {

Console.WriteLine("Sleeping");

}

public void Eat() { }

}

public class Antelope : Animal {}

public class Lion : Animal {

public void StalkPrey() { }

}

public class Elephant : Animal {

public int CarryCapacity;}

Uses:

Elephant e = new Elephant();

e.Sleep(); 

Page 71: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 71/114

Page 72: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 72/114

You cannot derive from a sealed class

Prevents the class from being overridden orextended by third parties

public sealed class Elephant {

...

}

Page 73: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 73/114

Introduction C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application

Conditional & Iteration Statement Class, Method & Constructor

XML Comments

Static Class Member

Designing Object

Inheritance Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 74: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 74/114

Polymorphism is an object-oriented conceptthat enables you to treat your derived classesin a similar manner, even though they aredifferent.

 When you create derived classes, you providemore specialized functionality;polymorphism enables you to treat these new

objects in a general way. 

Page 75: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 75/114

Page 76: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 76/114

A virtual method is one whose implementationcan be replaced by a method in a derived class.

Use the keyword virtual, in the base class

method Use the override keyword, in the derived

class method.

When you override a virtual method, theoverriding method must have the samesignature as the virtual method.

Page 77: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 77/114

public class Animal {public virtual void Eat() {

Console.WriteLine("Eat something");

}

}

public class Cat : Animal {

public override void Eat() {Console.WriteLine("Eat small animals");

}

}

public void FeedingTime( Animal someCreature ) {

if ( someCreature.IsHungry ) {

someCreature.Eat();

}

}

Cat myCat = new Cat();

FeedingTime(myCat);

Page 78: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 78/114

The base keyword is used in derived classes to accessmembers of the base class.public class Animal {

public virtual void Eat() {

Console.WriteLine("Eat something");

}}

public class Cat : Animal {

public void StalkPrey() { }

public override void Eat() {

base.Eat();

Console.WriteLine("Eat small animals");

}

}

Page 79: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 79/114

An abstract class is a generic base class Contains an abstract method that must be

implemented by a derived class.

An abstract method has no implementation inthe base class

Can contain non abstract members

Any class that contains abstract members must

be abstract

Page 80: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 80/114

public abstract class Animal {public abstract void Eat();

}

public class Mouse : Animal {

public override void Eat() {

Console.WriteLine("Eat cheese");

}

}

Page 81: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 81/114

Page 82: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 82/114

A data structure that contains a number ofvariables called element of the array.

All the array elements must be of the same

type. Arrays are zero indexed.

Arrays can be:

Single- dimentional, an array with the rank one.

Multidimentional, an array with the rank more thanone

 Jagged, an array whose elements are arrays

Page 83: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 83/114

Method Description

Sort  Sorts the elements in an array

Clear  Sets a range of elements to zero or null 

Clone  Creates a copy of the array

GetLength  Returns the length of a given dimensionIndexOf  Returns the index of the first occurrence of a value

Length  Gets the number of elements in the specifieddimension of the array

Page 84: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 84/114

Declare the array by adding a set of squarebrackets to the end of the variable type of theindividual elements

int[] MyIntegerArray;

Instantiate to create

int[] numbers = new int[5];

To create an array of type object

object[] animals = new object[100];

Page 85: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 85/114

Initialize an arrayint[ ] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

numbers[4] = 5;

Accessing array membersstring[] animals = {"Elephant", "Cat", "Mouse"};

Animals[1]= “cow”; 

String someAnimal = animals[2];

Page 86: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 86/114

Using foreeach statement repeats theembedded statement(s) for each elements inthe arrays.

int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0};

foreach (int i in numbers) {

Console.WriteLine(i);

}

Page 87: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 87/114

Page 88: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 88/114

  params keyword used to pass a variablenumber of arguments to method

class ParamExample {

public string Combine(string s1, string s2,

params object[] others) { 

string combination = s1 + " " + s2;foreach ( object o in others ) {

combination += " " + o.ToString();

}

return combination;

}

}

You can use this method as follows:

string combo = pe.Combine("One", "two", "three", "four" );

// combo has the value "One two three four"

combo = pe.Combine("alpha", "beta");

// combo has the value "alpha beta"

Page 89: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 89/114

When a class contains an array, or a collection,it is useful to access the information as thoughthe class itself were an array.

An indexer is a property that allows you toindex an object in the same way as an array.

Page 90: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 90/114

  public class Zoo {private Animal[] theAnimals;

public Animal this[int i] { 

get {return theAnimals[i];} 

set { theAnimals[i] = value;} 

}

public Zoo() {

theAnimals = new Animal[100];

}}

public abstract class Animal {

abstract public void Eat();

}

public class Lion : Animal {

public override void Eat() { }

}public class Elephant : Animal {

public override void Eat() { }

}

public class Antelope : Animal {

public override void Eat() { }

Page 91: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 91/114

  class ZooKeeper {static void Main(string[] args) {

Zoo myZoo = new Zoo();

myZoo[0] = new Elephant(); 

myZoo[1] = new Lion(); 

myZoo[2] = new Lion(); 

myZoo[3] = new Antelope(); 

Animal oneAnimal = myZoo[3];}

}

Page 92: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 92/114

Lists, Queues, Statcks and Hash Tables arecommon way to manage data in an application

List: A collection that allows you access by index.Example: An array is a list, an ArrayList is a list

Queue: First in first out collection of objects.Example: Waiting in line at a ticket office.

Stack: Last in first out collection of objects.Example: a pile of plates

Hash Tables: Represents a collection of associatedkeys and values organized around the hash codeof the key. Example: Dictionary

Page 93: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 93/114

ArrayList does not have fixed size; it grows asneeded

Use Add(object) to add an object to the end of

the ArrayList Use [] to access elements of the ArrayList.

Use TrimToSize() to reduce the size to fit thenumber of elements in the ArrayList

Use clear to remove all the elements

Can set the capacity explicitly

Page 94: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 94/114

Page 95: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 95/114

Page 96: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 96/114

  public class ZooKeeper {static void Main(string[] args) {

Zoo myZoo = new Zoo();

myZoo.ZooAnimals.Add( new Lion() );

myZoo.ZooAnimals.Add( new Elephant() );

myZoo.ZooAnimals.Insert( 1, new Lion() );

Animal a = myZoo[0]; 

myZoo[1] = new Antelope(); 

}

}

Page 97: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 97/114

Queues: first in, first out Enqueue places object in the Queue

Dequeue removes objects from the Queue.

Stacks: last in, first out Push places object in the stack

Pop removes object from the stack

Counts get the number of objects contained in a

stack or queue

Page 98: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 98/114

using System;using System.Collections;

class Message {private string messageText;public Message (string s) {

messageText = s;}public override string ToString() {

return messageText;

}}

class Buffer {private Queue messageBuffer;public void SendMessage( Message m ) {

messageBuffer.Enqueue( m );}public void ReceiveMessage( ) {

Message m = (Message) messageBuffer.Dequeue();

Console.WriteLine( m.ToString() );}public Buffer() {

messageBuffer = new Queue();}

}

Page 99: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 99/114

  class Messenger {static void Main(string[] args) {

Buffer buf = new Buffer();

buf.SendMessage( new Message("One") );

buf.SendMessage( new Message("Two") );

buf.ReceiveMessage ();

buf.SendMessage( new Message("Three") );

buf.ReceiveMessage ();buf.SendMessage( new Message("Four") );

buf.ReceiveMessage ();

buf.ReceiveMessage ();

}

}

The preceding code produces the following output:

One

Two

Three

Four

Page 100: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 100/114

using System;using System.Collections;

class Message {private string messageText;public Message (string s) {

messageText = s;}public override string ToString() {

return messageText;

}}

class Buffer {private Stack messageBuffer;public void SendMessage( Message m ) {

messageBuffer.Push( m );}public void ReceiveMessage( ) {

Message m = (Message) messageBuffer.Pop();

Console.WriteLine( m.ToString() );}public Buffer() {

messageBuffer = new Stack();}

}

Page 101: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 101/114

class Messenger {static void Main(string[] args) {Buffer buf = new Buffer();buf.SendMessage( new Message("One") );buf.SendMessage( new Message("Two") );buf.ReceiveMessage ();buf.SendMessage( new Message("Three") );

buf.ReceiveMessage ();buf.SendMessage( new Message("Four") );buf.ReceiveMessage ();buf.ReceiveMessage ();

}}

The preceding code produces the following output:TwoThreeFourOne

Page 102: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 102/114

A hash table is a data structure that is designedfor fast retrieval

Book b2 = new Book("Inside C#", 0735612889 );

myReferences.bookList.Add(b2.ISBN, b2);

Book b = (Book) myReferences.bookList[0735612889]; 

Console.WriteLine( b.Title );

Page 103: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 103/114

Introduction

C# Fundamentals

C# Pre defined Types

C# Expressions

Debugging Application

Conditional & Iteration Statement Class, Method & Constructor

XML Comments

Static Class Member

Designing Object

Inheritance Polymorphism

Arrays & Collections

Interface

Exception Handling

Page 104: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 104/114

Interface is a reference type that defines contact Specifies the members that must be supplied

by classes or interfaces that implement the

interface Can contain method, properties, indexers,

event.

Does not provides implementation for the

member Can inherits zero or more members

Page 105: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 105/114

interface ICarnivore {bool IsHungry { get; }Animal Hunt();void Eat(Animal victim);

}

public class Lion: ICarnivore {private bool hungry;public bool IsHungry {

get {

return hungry;}

}

Page 106: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 106/114

interface ICarnivore {bool IsHungry { get; }

Animal Hunt();

void Eat(Animal victim);

}

interface IHerbivore {

bool IsHungry { get; }

void GatherFood();

}

interface IOmnivore: IHerbivore, ICarnivore {void DecideWhereToGoForDinner();

}

Page 107: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 107/114

Abstract Class Is a special kind of class that cannot be instantiated An abstract class is only to be sub-classed(inherited from) The advantage is that it enforces certain hierarchies for all the

subclasses It is a kind of contract that forces all the subclasses to carry on the

same hierarchies or standards Interface

An interface is not a class It is an entity that is defined by the word Interface Interface has no implementation; it only has the signature

The main difference between them is that a class can implementmore than one interface but can only inherit from one abstract class Since C# doesn’t support multiple inheritance, interfaces are used

to implement multiple inheritance.

Page 108: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 108/114

Introduction C# Fundamentals C# Pre defined Types C# Expressions Debugging Application Conditional & Iteration Statement Class, Method & Constructor XML Comments Static Class Member Designing Object Inheritance Polymorphism Arrays & Collections Interface Exception Handling

Page 109: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 109/114

An exception is any error condition or unexpectedbehavior that is encountered by an executing program

try {

byte tickets = Convert.ToByte(numberOfTickets.Text);}

catch (FormatException) {

MessageBox.Show("Format Exception: please enter a number");

}

Page 110: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 110/114

try {byte tickets = Convert.ToByte(numberOfTickets.Text);

}

catch (FormatException e) {

MessageBox.Show("Format Exception: please enter a number");

}

catch (OverflowException e) {

MessageBox.Show("Overflow: too many tickets");

}

Page 111: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 111/114

A finally block is always executed, regardless of whether anexception is thrown.

FileStream xmlFile = null;

try {

xmlFile = new FileStream("XmlFile.xml", FileMode.Open);}

catch( System.IO.IOException e ) {

return;

}

finally {

if ( xmlFile != null ) {

xmlFile.Close();

}

}

Page 112: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 112/114

You can create your own exception classes by deriving from theApplication.Exception class

class TicketException: ApplicationException {

private bool purchasedCompleted = false;

public bool PurchaseWasCompleted {

get {return purchasedCompleted;}

}

public TicketException( bool completed, Exception e ) : base ("Ticket Purchase Error", e ){

purchasedCompleted = completed;

}}

The ReadData and run_ Click methods can use TicketException:

private int ReadData() {

byte tickets = 0;

try {

tickets = Convert.ToByte(textBox1.Text);

}

catch ( Exception e ) {

// check if purchase was complete

throw ( new TicketException( true, e ) );

}

return tickets;

}

Page 113: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 113/114

private void run_Click(object sender, System.EventArgs ea) {int tickets = 0;

try {

tickets = ReadData();

}

catch ( TicketException e ) {MessageBox.Show( e.Message );

MessageBox.Show( e.InnerException.Message );

}

}

Page 114: c Sharp Part i Mahedeev1 130718012115 Phpapp01

8/10/2019 c Sharp Part i Mahedeev1 130718012115 Phpapp01

http://slidepdf.com/reader/full/c-sharp-part-i-mahedeev1-130718012115-phpapp01 114/114