You are on page 1of 68

Basic .

NET Programming Using C# Day 1

ER/CORP/CRS/LA1007/003
Copyright 2007, Infosys Technologies Ltd

Ver. No.:1.0

Course Objectives
To understand .NET technology and .NET framework To gain ability to work in Visual Studio 2005 Team System IDE To learn Object Oriented Concept and its use in C# To gain ability to do windows programming and event handling Usage of built-in testing and code analysis tools in Visual Studio 2005 Team System for coding standards and testing in .NET framework

What is C# and where C# is being used


It is one of the most powerful object oriented programming language from Microsoft It enables programmers to build a wide range of applications for the Microsoft .NET platform The type of application that can be developed are:
Console Application Windows Application ASP.NET Web Application Web Service Mobile Web Application Class Library Windows Control Library Web Control Library Smart Device Application

Course Pre-requisites
Fundamental programming skills C as a programming language

Expectations
At the end of the Basic .NET Programming using C# module, the candidate should be conversant with the following
.NET 3.0 architecture Object Oriented Concepts Development of console application, windows application and class libraries Usage of built-in testing and code analysis tools in Visual Studio 2005 Team System for coding standards and testing in .NET framework Working on Visual Studio 2005 Team System

Understanding of C# would help in better understanding of the following modules


ADO.NET Advanced .NET Programming using C# ASP.NET

Course Agenda (1 of 3)
Session Plan - Day 1
Introduction to .NET Framework 3.0 technology Structure of C# program Data types in C# Introduction to class and object Arrays Introduction to Visual Studio 2005 Team System IDE Debugging using Visual Studio 2005 Team System Classes and objects this keyword Static data member, method, constructor Property Indexer Inheritance, Method overloading

Session Plan - Day 2

Course Agenda (2 of 3)
Session Plan - Day 3
Method overriding, Dynamic Polymorphism Abstract class and method Interface Introduction to namespace System.Object Boxing and unboxing Typecasting Destructor, IDisposable interface

Session Plan - Day 4


Exception handling Collection
Array List, SortedList and enumerator

Generics
Class Method Collection

Iterators Constraints
7

Course Agenda (3 of 3)
Session Plan - Day 5
GUI - Windows Application Partial Classes Delegates Anonymous methods Events Attributes

Session Plan Day 6


Development of assemblies Private and shared assemblies Versioning and side-by-side execution File Handling Serialization

References
www.msdn.com http://www.c-sharpcorner.com/ Troelsen,Andrew, C# and the .Net Platform , Apress Archer,Tom , Inside C#, Wrox Publication Sharp, John, Microsoft Visual C# 2005 Step by Step, Microsoft, 2005

Session Plan for Day1


Introduction to .NET Framework 3.0 technology Structure of C# program Data types in C# Introduction to class and objects Arrays Introduction to Visual Studio 2005 Team System IDE

11

Introduction to .NET Technologies

Copyright 2007, Infosys Technologies Ltd

Why .NET?
Need to create a new project re using

Marketing Module of ABB Project developed in VB.Net

Inventory Module of XYZ Project developed using Java

Sales Module of EZEE Project developed using C++

Applications developed in different languages. Incorporating them in a single project will be a NIGHTMARE!

13

Why .NET?

A nightmare? Not anymore. Microsoft .NET is a simple solution to this seemingly nightmarish situation. Equipped with various new features, it provides you with interoperability

Interoperability
Components of applications written in one programming language can be used for applications developed in other programming languages

14

Need for .NET Framework


Problems with integration of application developed with multiple technologies Limited cross-language interoperability COM technology drawbacks
COM components registration, unloading Dynamic Link Library (DLL) Hell

Hence Microsoft .NET Framework a computing platform for developing distributed applications for the Internet.

15

15

Evolution of Technology
Positives of Internet Browsing Era: Global connectivity Simplicity Mainstream

Internet Browsing Era

GUI Era

Character PC Era Terminal Mainframe Era


19

Negatives of Internet Browsing Era: Read-only No user control Too many islands Keyboard only

Technology Drivers
Computing power
Still doubling every 12 months

Connectivity
Low cost, broad reach Internet and Wireless, broadband access

Device proliferation
Multiple devices - PDAs, Mobile phones

Internet Standards
XML-Based integration

20

The .NET Evolution


Application

Code and data structures

Before COM, applications were completely separate entities with little or no integration

21

The .NET Evolution

COM provides a way for components to integrate; However, each component must provide the plumbing and objects cannot directly interact

22

The .NET Evolution

With the .NET Framework common language runtime (CLR), components are built on a common substrate; No plumbing is needed and objects can directly interact

23

.NETs answers to these problems


Web services: Software As A Service
Operate locally or hosted Programmability Any place, any time, any device An industry initiative Based on cross-platform standards Data and services

Integration Is Intrinsic

Assembly Fundamental unit of development, deployment, execution, security and versioning A collection of one or more files grouped together to form a logical unit

24

Web Services

SOAP on Protocol 1

Client Language X in Platform M

Web Service

Language A in Platform B

SOAP on Protocol 2
Client Language Y in Platform N

25

Web Services
A programmable application component accessible via standard Web protocols Web Services will allow people, companies, customers, suppliers, doctors, patients etc. to interact using different computers, different operating systems, and different applications

The center of the .NET architecture


Exposes functionality over the Web

26

.NET Design Goals Simplify Processes


simplify the process of application development, deployment, execution, and maintenance

Provide a Common Model


provide a common programming model so that the components written in one language can be used and extended by the components written in other .NET-compatible languages.

Provide Security
provide a high level of Code Access Security that enables administrators and developers to authorize applications similar to the way they have always been able to authorize users

Ensure Acceptability
Using web standards and best practices to ensure the maximum acceptability of the applications developed

27

.NET
What is .NET? The set of Microsoft software technologies Connecting the world of information, systems, people and devices. Small applications communicating with other applications
Located on the same machine or Via the Internet.

What is .NET Framework? A new computing platform Simplifies application development in the highly distributed environment of Internet

28

.NET Development Platform


Development Tools: Visual Studio 2005, Notepad

Web Clients

Protocols:HTTP, HTML, XML, SOAP, UDDI

Web Forms

Web Services

Components Microsoft .NET Framework

Consumer Applications

Windows OS

Internal/external Web Services

Microsoft .NET Foundation Web Services

Microsoft .NET enterprise Web Services

29

.NET Development Platform


Web Services

Enables software as a service Based on XML SOAP is a W3C standard. Strong third party support
Web Services XML, SOAP, HTTP, HTML
31

IBM, Ariba, Commerce One, Iona, Rogue Wave

.NET Development Platform


.NET Framework

Foundation for building, deploying, and running Web Services Productive, extensible, multilanguage Integration through public Internet standards
32

.NET Framework

Web Services XML, SOAP, HTTP, HTML

.NET Languages

COBOL Jscript .NET Eiffel Haskell / Mondrian Pascal ML APL Alice

C Visual C++ Visual Basic Visual C# Smalltalk Oberon Scheme Mercury Python

33

.NET Development Platform Visual Studio.NET

Visual Studio.NET

Most productive tool for developing Web Services, applications Unified programming model, IDE Builds on todays capabilities

.NET Framework

Web Services XML, SOAP, HTTP, HTML


34

.NET Development Platform


Microsoft .NET Development Platform

Partners
ADA
APL COBOL Pascal Eiffel Visual Perl Visual Python Smalltalk and many more

Customers, Partners Delivering Web Services


3rd Party Languages and Tools

Visual Studio.NET

3rd Party Infrastructure

.NET Framework

Web Services XML, SOAP, HTTP, HTML

35

Features of .NET

Single programming model

Distributed Systems

Integrity with COM

Rich User Interface

Portability with compiled applications

Easy Deployment

Extensibility

Multiple Language support

36

.NET 3.0 Architecture


Was formerly called WinFX Available for Windows Vista, Windows XP SP2 and Windows Server 2003 Built on the basic architecture of .NET 2.0 architecture Includes a new set of managed code APIs

37

.NET Framework 2.0 Architecture


VB C++ C# JScript
Visual Studio 2005 Team System

Common Language Specification ASP.NET Windows Forms

ADO.NET and XML

Base Class Library


Common Language Runtime Windows COM+ Services

38

.Net Languages and Execution Model


Source code
VB Compiler C# Compiler Assembly IL Code C++ Compiler Assembly IL Code

Managed code

Assembly IL Code

Common Language Runtime


JIT Compiler
Native Code

Operating System Services

40

Start

CLR Execution Model


Source Code
Development Machine

N CLR Header Y CLR Services

OS

Compiler

EXE/DLL (IL)

Policy
Class Libraries (IL)

IS Permissions?

Class Loader
Target Machine

Security Exception

Y Is Type Safe ?
Y N Is PreJIT ? JIT N Type Safe Exception

JIT Compiler

Managed Native Code

Call to an uncompiled method

Security Check

Execution

Y Execution IL
41

Features of CLR
Automatic Memory Management
Provides garbage collection

Language Interoperability
Platform Independence

Security Management
Provides Code Access Security (CAS) Type Safety

42

Common Type System


The CLR defines the type system, CTS All .NET languages map into the CTS CLR types defined under the system namespace All .NET languages provide their own keywords that map to the underlying CTS types
C# int keyword = System.Int32 VB.NET integer keyword = System.Int32

44

Common Language Specification (CLS)


CLS is a set of basic language guidelines that specify the features needed by .NET applications to fully interact with other objects regardless of their implementation language. Created to improve language interoperability
Defines a subset of the CTS Most of the Framework Class Libraries are CLS compliant Cannot overload based on return type Unsigned integer types are not allowed on public methods Arrays index has to start from 0.

45

.NET 3.0 Architecture


Development Tools: Visual Studio 2005, Notepad Protocols:HTTP, HTML, XML, SOAP, UDDI Web Forms Web Services

Web Clients

Components Microsoft .NET Microsoft .NET Framework Framework Windows OS

Consumer Applications

Internal/externalMicrosoft .NET Foundation Microsoft .NET enterprise Web Services Web Services Web Services

.NET 3.0 Architecture

Four new technologies in .NET Framework 3.0


Windows Card Space (WCS) (formerly, Infocard)
Technology to simplify and improve the safety of accessing resources and sharing personal identity information online.

Windows Presentation Foundation (WPF) (formerly, Avalon)


Provides classes for building UI, documents, and media

48

48

Four new technologies in .NET Framework 3.0


Windows Communication Foundation (WCF) (formerly, Indigo)
Unified programming model and runtime for building service oriented applications

Windows Workflow Foundation (WF)


Programming model, engine, and tools to build workflow-enabled applications that model business processes

49

49

Can you answer these questions?


What are the features of .NET framework? What are the components of .NET architecture? What are main functionalities of CLR? What is the difference between CTS and CLS? What do you mean by CLS compliant code? What is the main functionality of
WCS WCF

WPF
WF

50

Introduction to programming using C#

Copyright 2007, Infosys Technologies Ltd

Features of C Sharp
Object-oriented Simpler language
Compared to earlier OO languages like C++, it is simple Designed considering the pitfalls of earlier languages

Robust Architecture Neutral / Portable Secure


Built -in security features like absence of pointers and confinement of the C# program within its runtime environment

Support for Multithreading at language level Designed to handle Distributed applications

52

Introduction to Class and Objects

Copyright 2007, Infosys Technologies Ltd

What is a Class ?
A Class
Is a blue print used to create objects. Is a software template that defines the methods and variables to be included in a particular kind of Object.

Examples :
Animal, Human being, Automobiles, Bank Account, Customer

54

What is an Object ?
An object
Is an unique, identifiable, self-contained entity that contains attributes and behaviors. A software object is modeled after real world objects
A software object is a representative of the real world object

Can be viewed as a "black box" which receives and sends messages


Examples
Car Telephone Pen etc

55

Class Contains ..
State (Member variables)
The internal state of the object represented by values stored in member

variables
Variables defined inside a class form the state of the class Not exposed to external world

Behavior (Member Methods)


Behavior exhibited by the class to external world Functions defined inside the class form the behavior of the class Exposed to external world

56

State and Behavior


Example: Car object
State
Current Speed Current Gear Engine State (Running, Not Running)

Example: Dog Object


State
Color Breed Activity (Barking/Not barking) Tail Activity (Wagging/Not Wagging)

Behavior (Acts on the object and changes state)


Slow down Accelerate Stop Switch Off Engine Start Engine

Behavior
Bark Wag Tail Eat

57

Example: Objects and Classes

object

class Class Employee Name Age

EmpNo
setName()
Jodie 4001 Daria 4002 Jane 4003 Brittany 4004

setEmpNo() CalcSalary()

58

Structure of a C# program
using System;

class FirstProgram
{ public static void Main() { Console.WriteLine(Hello World); } }

<<Demo : Structure of C# program >>


59

Data Types in C#
Value Types Reference Types

Value types include simple types like char, int, and float, enum types, and struct types Variable holds the actual value
Allocated on stack

Reference types include class types, interface types, delegate types, and array types Variable holds memory location
Allocated on heap

Assignment of one value type to another copies the value

Assignment of one reference type to another copies the reference

60

Data Types Value Types


Data Types

Integer

Floating Point
float

Character

Boolean

byte sbyte

Char

bool

double short decimal ushort int uint long ulong

61

A structure of a Class in C#
class ClassName { DataType1 MemberVariable1; ClassType2 Object2; /* A data members can be defined using 1) primitive types (int, float, string ) 2) user defined structures 3) object reference of a class */ ClassName( parameters ) { /* The constructor is used for members initialization during the object creation */ } return-type MethodName( parameters ) { /* All the application logics have to be placed inside the methods. A method must be placed inside a class.*/ } }// End of the class

63

Object creation & initialization in C# (1 of 4)


class Employee { // Assume that class members are placed here } /*To create an object of Employee Class, Any of the following approach can be followed Both approaches results the same */ // Approach 1 Employee emp; // object reference emp is created emp = new Employee(); // object is created & assigned to emp // Approach 2 Employee emp = new Employee();

<<Demo : Object creation & initialization>>


64

Object creation & initialization in C# (2 of 4)


class Employee { public int age; public void PrintEmpDetails(){ /* logic to print details*/ } public int CalculateSalary(){/*logic to calculate salary /* } } // end of Employee class class Manager { void SomeMethodOfManager() { /* Accessing members of an Employee object outside the Employee class */ Employee emp = new Employee(); emp.age = 22;//objectRefName.DataMemeberName = Value; emp.PrintEmpDetails();//ObjectRefName.MethodName(params); int salary = emp.CalculateSalary(); } } // end of Manager class <<Demo : Object creation & initialization>>
65

Object creation & initialization in C# (3 of 4)


class Employee { public int age; public float salary; } emp1 Unassigned Employee emp1; /* emp1 (an object reference) is initially unassigned. It can point any one Employee type object or null */ age = 0 emp1 = new Employee(); emp1 salary = 0.0 /* During object creation, new() allocates heap memory and the constructor initializes the objects members */ Employee emp2; emp2 Unassigned /* During object reference assignment of emp1 to emp2, emp2 is made to point the same object pointed by emp1 */ emp2 = emp1; emp2 emp2.age = 20; // emp1.age is also 20
66

Object creation & initialization in C# (4 of 4)


class Employee { private int _age; } // end of Employee class class Manager { void SomeMethodOfManager() { /* Accessing members of an Employee object outside the Employee class */ Employee emp = new Employee(); emp._age = 22; //results in compiler error. /* A private member cant be accessed outside the class */ } } // end of Manager class

67

Predefined Types - String


A sequence of characters Strings are immutable (once created the value does not change) String is Reference type and string class in C# is an alias of System.String class in the dot net framework The following special syntax for string literals is allowed as they are built in data types.

String str = I am a string;


C# Type System Type

String

System.String

68

Predefined Types - StringBuilder


Every time some modifications are done to the string a new String object needs to be created StringBuilder class can be used to modify string without creating a new string Properties:
Length

Methods
Append() Insert() Remove() Replace()

StringBuilder str = new StringBuilder (hi); str.Append(how are you?) str.Insert(6,How do you do?);
69

Arrays
Every array in C# is an object Two ways of creating one dimensional array
/* Creating the array reference and initializing it in two steps */ Type [ ] arrayObj; arrayObj = new Type[ capacity ]; /* Creating the array reference and initializing it in a single step */ Type [ ] arrayObj = new Type[ capacity ];

Two ways of initializing array elements during the Array object creation
Type [ ] arrayObj; arrayObj = new Type[ ] { value1, value2, value3, }; Type [ ] arrayObj = { value1, value2, value3, }; <<Demo : Arrays>>
70

Can you answer these questions?


Mention the members that a class can have. What are the different ways that an object can be created? What is the difference between object creation and object initialization? Why we need to declare an array? How an array object is created and initialized?

Questions on Self study topics What do you mean by jagged arrays? Which feature is different in the switch statement in C# as compared to in C?

71

Using Visual Studio 2005 Team System IDE


Microsoft Visual Studio 2005 Team System is the Integrated Development Environment (IDE) This helps in fast development of Microsoft applications on .NET platform We can develop Console application as well as Windows Application in C# using Visual Studio Visual Studio 2005 Team System provides automated unit testing and code analysis

72

Visual Studio 2005 Team System

Developer Solution Architect Tester

Infrastructure Architect

Project Manager

Design for Operations Increased Reliability


73

Predictability & Visibility Quality Early & Often

Using Visual Studio 2005 Team System IDE

74

Summary
Introduction to .NET technology Structure of C# program Data types in C# Introduction to class and objects Arrays Introduction to Visual Studio 2005 Team System IDE

75

Thank You
The contents of this document are proprietary and confidential to Infosys Technologies Ltd. and may not be disclosed in whole or in part at any time, to any third party without the prior written consent of Infosys Technologies Ltd. 2006 Infosys Technologies Ltd. All rights reserved. Copyright in the whole and any part of this document belongs to Infosys Technologies Ltd. This work may not be used, sold, transferred, adapted, abridged, copied or reproduced in whole or in part, in any manner or form, or in any media, without the prior written consent of Infosys Technologies Ltd.

Copyright 2007, Infosys Technologies Ltd

You might also like