You are on page 1of 52

Week 1: Introduce

C#4.0
What is the .NET Framework ?
CLR, MSIL,IL,FLC

Garbage collection

Namespace

What C# is and how it relates to the


.NET Framework
WHAT IS THE .NET FRAMEWORK?
Microsofts modern software development
platform
Programs executed by Common Language
Runtime (CLR)
FLC(Framework class library) Includes a large
library of components (classes) which can be
used in programs
Supports several programming languages,
including C#, Visual Basic, C++, J#

C#4.0 Windows Programming 1 Chapter 1: Introducing C# Slide 2


.NET Framework

C#4.0 Windows Programming 1


3
Chapter 1: Introducing C# Slide
.NET Framework .NET 4.0 (2010)
VS.NET 2010
Default: Windows 7
.NET 3.5 (Nov-2007)
VS.NET 2008
Default: Windows 7

.NET 2.0 (Nov-2005)


VS.NET 2005
.NET 3.0 (Nov-2006)
Default: Windows
Vista, Server 2008
.NET 1.1 (Apr-2003)
VS .NET 2003
Default: Server 2003
.NET 1.0
(Feb-2002)
C#4.0 4
Writing Applications Using the .NET
Framework
CLR: Common Language Runtime
MSIL or IL: Microsoft Intermediate Language
JIT:
just-In-Time compiler
Assemblies

Managed Code
Garbage Collection

C#4.0 Windows Programming 1 Chapter 1: Introducing C# Slide 5


.NET Framework -CLR
Common Language Runtime Architecture

C#4.0 Windows Programming 1 Chapter 1: Introducing C# Slide 6


Writing Applications Using the .NET
Framework

CT
C#.NET
C# .NET Framework
Compiler

Programmer
CT VB .NET
VB.NET Compiler
0
101101
0
MS IL 101101
0
CLR 101101
CT J# .NET
J#.NET Compiler

C++
CT
Compiler
C++.NET

C#4.0 7
Writing Applications Using the .NET
Framework

C#4.0 Windows Programming 1 Chapter 1: Introducing C# Slide 8


Assembly

Managed Modules
b ly
Cha IL code thc thi em
Ass
Cha T
resource, metadata .N
E

Portable Executable (PE) EXE


Process assembly (EXE)
Library assembly (DLL) DLL

C#4.0 9
Managed Code

Software
that is written using the .NET
Framework is called Managed

Managed Executable
Legacy Software
(unmanaged code)
Common Language Runtime

Windows
(OS khc)

C#4.0 10
Garbage collection

class myClass{
Managed Heap
void Method(){
variable v1;
variable v2; B
A
do{
. E
C
}
D
}

A v D s b xo do
ko cn tham chiu
hay truy cp ti

C#4.0 11
Namespaces
Namespaces are also used as a means of
categorizing items in the .NET Framework
Qualified names use period characters (.)
between namespace levels
Withina namespace, you can define
nested namespaces, also using the
namespace keyword.

C#4.0 Windows Programming 1


Expressions Slide 12
Chapter3: Variables and
Namespaces

C#4.0 Windows Programming 1


Expressions Slide 13
Chapter3: Variables and
Namespace
Namespace Description
System Cha lp ton hc, chuyn i d liu
System.IO Cc lp cho thao tc Input v Output
System.Net Cc lp lin quan n network protocol
System.Collections Chc cc lp lin quan n x l tp hp

System.Data Cc lp ca ADO.NET
System.Drawing Cc lp thc thi chc nng GUI
System.Threading Cc lp lp trnh MultiThread
System.Web Cc lp lin quan n HTTP protocol
System.Xml Cc lp lin quan XML

C#4.0 14
Framework (Base) Class Library FCL/BCL
c tnh li: IL, kiu d liu trong CTS
H tr Win GUI v control
WebForm (ASP.NET)
Data Access (ADO.NET)
Directory Access
File System, registry access
Networking and web browsing
.NET attributes and reflection
WinOS access
COM interoperability

C#4.0 15
Run .NET App from Client
Required MS .NET Framework compatible
MS .NET Framework 2.0, 3.5
Install .NET 3.5 Full Redistributable package
(dotnetfx35.exe, 197MB)
Windows Vista comes with .NET Framework
3.0
Windows 7 comes with .NET Framework 4.0

. .NET .NET
NET App App
App

C#4.0 16
C# Language

C#4.0 17
WHAT IS C#?
Applications You Can Write with C#
Windows applications
Web applications
Web services:

C#4.0 Windows Programming 1 Chapter 1: Introducing C# Slide 18


Visual Studio
Powerful, professional Integrated
Development Environment (IDE)
Integrates compilers, debugger and many
other useful tools for development
Can work with many different types of project,
including:
Console (text-based) applications
Windows (GUI) applications
Web applications (ASP.NET)
Class libraries
C#4.0 Windows Programming 1 Chapter 1: Introducing C# Slide 19
Visual Studio

Solution explorer

Visual designer

Toolbox windows

Properties windows

C#4.0 Windows Programming 1 Chapter 1: Introducing C# Slide 20


Visual Studio projects
A project contains source code files, settings
and resources for an application
May contain references to class libraries

May contain data used by application

Building a project:
Compiles source files
Copies non-source files to output folder
Creates an assembly in output folder
Building a solution builds all its projects

C#4.0 Windows Programming 1 Chapter 1: Introducing C# Slide 21


Project details
Solution folder contents

Project folder contents

References class libraries


used by this application

Solution file (.sln) and project file


(.csproj) are created by VS and
contain solution/project
configuration information

C#4.0 Windows Programming 1 Chapter 1: Introducing C# Slide 22


Creating a Visual Studio project
Demo

C#4.0 Windows Programming 1 Chapter 1: Introducing C# Slide 23


Week 1: THE C# LANGUAGE C#4.0
Chapter 2: Writing a C#
Program
A basic working knowledge of Visual Studio
2010 and Visual C# 2010 Express Edition
How to write a simple console application
How to write a Windows Forms application
Visual C# 2010 Ultimate

C#4.0 Windows Programming 1


Slide 25
Chapter 2: Writing a C# Program
CONSOLE APPLICATIONS

C#4.0 Windows Programming 1


Slide 26
Chapter 2: Writing a C# Program
WINDOWS FORMS APPLICATIONS
MO_BAI1

C#4.0 Windows Programming 1


Slide 27
Chapter 2: Writing a C# Program
The Solution Explorer

C#4.0 Windows Programming 1


28
Chapter 1: Introducing C# Slide
The Properties Window

C#4.0 Windows Programming 1


29
Chapter 1: Introducing C# Slide
Code view

C#4.0 Windows Programming 1


30
Chapter 1: Introducing C# Slide
The Error List Window

C#4.0 Windows Programming 1


31
Chapter 1: Introducing C# Slide
SUMMARY

???
C#4.0 Windows Programming 1
Slide 32
Chapter 2: Writing a C# Program
Week 1: THE C# LANGUAGE
C#4.0
Chapter 3:Variables and Expressions
Basic C# syntax
Variables and how to use them
Expressions and how to use them
BASIC C# SYNTAX
The look and feel of C# code is similar to that
of C++ and Java.
C# compilers ignore additional spacing in
code, whether it results from spaces, carriage
returns, or tab characters (collectively known
as whitespace characters).
Statements

C# is a block-structured language, meaning


statements are part of a block of code.

C#4.0 Windows Programming 1


Expressions Slide 34
Chapter3: Variables and
block
These blocks, which are delimited with curly
brackets ({ and }), may contain any number of
statements, or none at all

C#4.0 Windows Programming 1


Expressions Slide 35
Chapter3: Variables and
comments
Comments can be created using //
Multi-lines comments use /* */

You can use single-line comments that start


with three / symbols instead of two
/// A special comment
Comments are ignored by the compiler
Used only for human readers

C#4.0 Windows Programming 1


Expressions Slide 36
Chapter3: Variables and
The code outlining
You can do this with the #region and
#endregion keywords, which define the start
and end of a region of code that can be
expanded and collapsed.

C#4.0 Windows Programming 1


Expressions Slide 37
Chapter3: Variables and
VARIABLES
C# syntax for declaring variables merely
specifies the type and variable name:
<type> <name>;
int intNumberOfStudents;
Declaration includes
Name, follow Naming Convention Rules
Data Type
Required Value for Constants
Optional Initial Value for Variables

C#4.0 Windows Programming 1


Expressions Slide 38
Chapter3: Variables and
Simple Types

C#4.0 Windows Programming 1


Expressions Slide 39
Chapter3: Variables and
Simple Types

C#4.0 Windows Programming 1


Expressions Slide 40
Chapter3: Variables and
Simple Types

C#4.0 Windows Programming 1


Expressions Slide 41
Chapter3: Variables and
Using Simple Type Variables
Mo_b3

C#4.0 Windows Programming 1


Expressions Slide 42
Chapter3: Variables and
Variable Naming
The first character of a variable name must be
either a letter, an underscore character ( _ ),
or the at symbol (@).
Subsequent characters may be letters,
underscore characters, or numbers.

C#4.0 Windows Programming 1


Expressions Slide 43
Chapter3: Variables and
String Literals

C#4.0 Windows Programming 1


Expressions Slide 44
Chapter3: Variables and
String Literals
This means that the following strings are
equivalent:
"Karli \s string."
"Karli \u0027 s string.
@ "A short list:

item 1
item 2
"C:\\Temp\\MyDir\\MyFile.doc

@ "C:\Temp\MyDir\MyFile.doc"
C#4.0 Windows Programming 1
Expressions Slide 45
Chapter3: Variables and
EXPRESSIONS
Operators can be roughly classified into three
categories:
Unary Act on single operands

BinaryAct on two operands

TernaryAct on three operands

C#4.0 Windows Programming 1


Expressions Slide 46
Chapter3: Variables and
Mathematical Operators

C#4.0 Windows Programming 1


Expressions Slide 47
Chapter3: Variables and
C#4.0 Windows Programming 1
Expressions Slide 48
Chapter3: Variables and
Manipulating Variables with
Mathematical Operators

C#4.0 Windows Programming 1


Expressions Slide 49
Chapter3: Variables and
Assignment Operators

C#4.0 Windows Programming 1


Expressions Slide 50
Chapter3: Variables and
Operator Precedence

C#4.0 Windows Programming 1


Expressions Slide 51
Chapter3: Variables and
SUMMARY

???
C#4.0 Windows Programming 1
Expressions Slide 52
Chapter 3: Variables and

You might also like