Date post: | 04-Mar-2015 |
Category: |
Documents |
Upload: | harsha-vardhan |
View: | 17 times |
Download: | 0 times |
Copyright © 2007, Infosys Technologies Ltd
Basic .NET Programming Using C# – Day 1
ER/CORP/CRS/LA1007/003 Ver. No.:1.0
2
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 it’s 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
3
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
4
Course Pre-requisites
• Fundamental programming skills• C as a programming language
5
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
6
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
• Session Plan - Day 2– Debugging using Visual Studio 2005 Team System– Classes and objects– this keyword– Static – data member, method, constructor– Property– Indexer – Inheritance, Method overloading
7
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
8
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
9
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
11
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
Copyright © 2007, Infosys Technologies Ltd
Introduction to .NET Technologies
13
Why .NET?
Need to create a
new project
Sales Module of
EZEE Project
developed using C++
Marketing Module of
ABB Project developed in VB.Net
Inventory Module of
XYZ Project
developed using Java
re – using
Applications developed in different languages.Incorporating them in a single project will be a
NIGHTMARE!
14
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
1515
• 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.
Need for .NET Framework
19
Evolution of Technology
Terminal Mainframe Era
GUI Era
Character PC Era
Internet Browsing Era
Negatives of Internet Browsing Era:
“Read-only”
No user control
Too many islands
Keyboard only
Positives of Internet Browsing Era:
Global connectivity
Simplicity
Mainstream
20
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
21
The .NET Evolution
Before COM, applications were completely separate entities with little or no integration
Application
Code and data structures
22
The .NET Evolution
COM provides a way for components to integrate; However, each component must provide the “plumbing” and objects cannot directly interact
23
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
24
.NET’s answers to these problems
Web services:• Software As A Service
– Operate locally or hosted
– Programmability
– Any place, any time, any device
• Integration Is Intrinsic– An industry initiative
– Based on cross-platform standards
– Data and services
Assembly• Fundamental unit of development, deployment, execution, security and
versioning • A collection of one or more files grouped together to form a logical unit
25
Web Services
Web Service
Language A in Platform B
Client
Language X in Platform M
Client
Language Y in Platform N
SOAP on Protocol 1
SOAP on Protocol 2
26
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
27
.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
28
.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
29
.NET Development Platform
Internal/externalWeb Services
Microsoft .NET FoundationWeb Services
Microsoft .NET enterpriseWeb Services
Web Clients
ConsumerApplications
Web Forms
Components
Windows OS
Web Services
Microsoft .NET Framework
Develo
pm
ent T
oo
ls:V
isual S
tud
io 2005, N
otep
ad
31
.NET Development Platform
Web ServicesWeb ServicesXML, SOAP, HTTP, HTMLXML, SOAP, HTTP, HTML
•Enables software as a service
•Based on XML
•SOAP is a W3C standard.
•Strong third party support
•IBM, Ariba, Commerce One, Iona, Rogue Wave
Web Services
32
.NET Development Platform
Web ServicesWeb ServicesXML, SOAP, HTTP, HTMLXML, SOAP, HTTP, HTML
.NET Framework.NET Framework
•Foundation for building, deploying, and running Web Services
•Productive, extensible, multi-language
•Integration through public Internet standards
.NET Framework
33
.NET Languages
• COBOL• Jscript .NET• Eiffel• Haskell / Mondrian• Pascal• ML• APL• Alice
• C• Visual C++• Visual Basic• Visual C#• Smalltalk• Oberon• Scheme• Mercury• Python
34
.NET Development Platform
Web ServicesWeb ServicesXML, SOAP, HTTP, HTMLXML, SOAP, HTTP, HTML
.NET Framework.NET Framework
•Most productive tool for developing Web Services, applications
•Unified programming model, IDE
•Builds on today’s capabilities
Visual Studio.NETVisual Studio.NET
Visual Studio.NET
35
.NET Development Platform
ADA
APL
COBOL
Pascal
Eiffel
Visual Perl
Visual Python
Smalltalk
and many more
Web ServicesWeb ServicesXML, SOAP, HTTP, HTMLXML, SOAP, HTTP, HTML
.NET Framework.NET Framework3rd Party
Infrastructure
3rd PartyLanguages and Tools Visual Studio.NETVisual Studio.NET
Customers, PartnersDelivering Web Services
PartnersMicrosoft .NET
Development Platform
36
Features of .NET
Distributed Systems
Rich User Interface
Easy Deployment
Multiple Language supportExtensibility
Portability with compiledapplications
Integrity with COM
Single programming model
37
.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
38
.NET Framework 2.0 Architecture
Windows COM+ Services
Common Language Runtime
Base Class Library
ADO.NET and XML
ASP.NET Windows Forms
Common Language Specification
VB C++ C# JScript …V
isua
l Stu
dio
20
05 T
ea
m S
ystem
40
.Net Languages and Execution Model
VBVBSource Source codecode
CompilerCompiler
C++C++C#C#
CompilerCompilerCompilerCompiler
Operating System ServicesOperating System Services
Common Language Runtime
JIT CompilerJIT Compiler
Native CodeNative Code
ManagedManagedcodecode
AssemblyAssemblyIL CodeIL Code
AssemblyAssemblyIL CodeIL Code
AssemblyAssemblyIL CodeIL Code
41
CLR Execution Model
Source Code
Compiler
EXE/DLL(IL)
De
velo
pm
ent
Mac
hin
e
Class LoaderClass Libraries
(IL)
JIT Compiler
Managed NativeCode
ExecutionSecurity Check
Call to anuncompiled
method
Tar
ge
tM
ach
ine
Start
CLR Header
CLR Services
ISPermissions?
N
Y
N
N
N
Y
Y
Y
OS
Policy
Type Safe Exception
JIT
Security Exception
IsPreJIT ?
Execution IL
IsType Safe ?
42
Features of CLR
• Automatic Memory Management– Provides garbage collection
• Language Interoperability
• Platform Independence
• Security Management– Provides Code Access Security (CAS)
– Type Safety
44
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
45
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.
.NET 3.0 Architecture
Internal/externalWeb Services
Microsoft .NET FoundationWeb Services
Microsoft .NET enterpriseWeb Services
Web Clients
ConsumerApplications
Web Forms
Components
Windows OS
Web Services
Microsoft .NET Framework
Develo
pm
ent T
oo
ls:V
isual S
tud
io 2005, N
otep
adMicrosoft .NET
Framework
.NET 3.0 Architecture
4848
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
4949
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
50
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
Copyright © 2007, Infosys Technologies Ltd
Introduction to programming using C#
52
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
Copyright © 2007, Infosys Technologies Ltd
Introduction to Class and Objects
54
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
55
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
56
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
57
State and Behavior
• Example: Car object– State
• Current Speed• Current Gear• Engine State (Running, Not Running)
– Behavior (Acts on the object and changes state)
• Slow down• Accelerate• Stop• Switch Off Engine• Start Engine
• Example: Dog Object– State
• Color• Breed• Activity (Barking/Not barking)• Tail Activity (Wagging/Not
Wagging)
– Behavior• Bark• Wag Tail• Eat
58
Example: Objects and Classes
Daria
4002
Jane
4003
Brittany
4004
Jodie
4001
classobject
Class Employee
Name
Age
EmpNo
setName()
setEmpNo()
CalcSalary()
59
Structure of a C# program
using System;
class FirstProgram
{
public static void Main()
{
Console.WriteLine(“Hello World”);
}
}
<<Demo : Structure of C# program >>
60
Data Types in C#
Value Types Reference Types
Value types include simple types like char, int, and float, enum types, and struct types
Reference types include class types, interface types, delegate types, and array types
Variable holds the actual value Variable holds memory location
Allocated on stack Allocated on heap
Assignment of one value type to another copies the value
Assignment of one reference type to another copies the reference
61
Data Types – Value Types
Data Types
Integer FloatingPoint Character Boolean
byte
sbyte
short
ushort
int
float
double
Char bool
uint
long
ulong
decimal
63
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
64
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 followedBoth approaches results the same */
// Approach 1Employee emp; // object reference ‘emp’ is created emp = new Employee(); // object is created & assigned to emp
// Approach 2Employee emp = new Employee();
<<Demo : Object creation & initialization>>
65
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>>
66
Object creation & initialization in C# (3 of 4)
class Employee {
public int age;
public float salary;
}
Employee emp1;
/* emp1 (an object reference) is initially unassigned.
It can point any one Employee type object or null */
emp1 = new Employee();
/* During object creation, new() allocates heap memory
and the constructor initializes the object’s members */
Employee emp2;
/* During object reference assignment of emp1 to emp2,
emp2 is made to point the same object pointed by emp1 */
emp2 = emp1;
emp2.age = 20; // emp1.age is also 20
age = 0salary = 0.0emp1
emp2 Unassigned
emp2
emp1 Unassigned
67
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 can’t be accessed outside the class */
}
} // end of Manager class
68
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
69
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?”);
70
Arrays
• Every array in C# is an object • Two ways of creating one dimensional array
• Two ways of initializing array elements during the Array object creation
/* 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 ];
Type [ ] arrayObj; arrayObj = new Type[ ] { value1, value2, value3, …}; Type [ ] arrayObj = { value1, value2, value3, …};
<<Demo : Arrays>>
71
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?
72
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
73
Visual Studio 2005 Team System
IncreasedReliability
QualityEarly & Often
Predictability& Visibility
Design forOperations
ProjectManager
SolutionArchitect
DeveloperTester
InfrastructureArchitect
74
Using Visual Studio 2005 Team System IDE
75
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
Copyright © 2007, Infosys Technologies Ltd
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.”