C Sharp
C Sharp
Study Notes
Owner: Satish Talim
File: C# - Console Applications
Last saved: 7/6/2001
Email Id. [email protected]
Version 3
Satish Talim 2001-2002, Study Notes. All Rights Reserved.
Notice:
This documentation is an early release of the final Study Notes, which may change substantially
prior to final release, and is information of Satish Talim.
This document is provided for informational purposes only and Satish Talim makes no warranties,
either express or implied, in this document. Information in this document is subject to change
without notice.
The entire risk of the use or the results of the use of this document remains with the user.
Complying with all applicable international copyright laws is the responsibility of the user.
Microsoft, Windows, Visual Basic, and Visual C++ are either registered trademarks or trademarks
of Microsoft Corporation in the U.S.A. and/or other countries.
Other product and company names mentioned herein may be the trademarks of their respective
owners.
-2002, Study Notes. All Rights Reserved.
Table of Contents
Table of Contents
1.
Introduction
............................................................................................................... 7 1.1 A
New Platform? ....................................................................................................... 7
1.2
System
Requirements................................................................................................ 7 1.3
Purpose of these Study Notes .....................................................................................
7
1.4
Who
can
use
these
Study
Notes?
................................................................................
7
1.5
Updates
to
this
document...........................................................................................
7
1.6
Recommended Sites on C# ........................................................................................
7
1.7
My
Workshops
on
C#.................................................................................................
8
1.8
Satish
Talim?............................................................................................................ 8 1.9
Acknowledgements....................................................................................................
8
2. C# Program
Elements............................................................................................... 10 2.1
Overview of the
.NET................................................................................................10 2.2 C# and
Java............................................................................................................11 2.3
Our first C# Program Hello,
world............................................................................11 2.4 Naming Guidelines
...................................................................................................15 2.4.1
Namespaces ......................................................................................................15
2.4.2
Classes..............................................................................................................15
2.4.3 Methods
............................................................................................................15 2.4.4
Method Arguments .............................................................................................16
2.4.5
Interfaces..........................................................................................................16
2.4.6 Class
members...................................................................................................16 2.5
Automatic memory management
...............................................................................16 2.6 Comments
..............................................................................................................16 2.7
Blocks.....................................................................................................................1
9 2.8 Separation
..............................................................................................................19 2.9
Whitespace
.............................................................................................................19 2.10
Keywords (74) .......................................................................................................19
2.11 Constants const / readonly
...................................................................................20 2.12
Variables...............................................................................................................20
2.13 Naming constants and
variables...............................................................................21 2.14 Escape sequences
..................................................................................................21 2.15 Statements
and Expressions ....................................................................................22 2.15.1
Empty statement ..............................................................................................22
2.16
Types....................................................................................................................22
2.17 Predefined types
....................................................................................................25 2.18
Operators..............................................................................................................30
2.18.1 checked and unchecked
operators.......................................................................33 2.19 Operator overloading
..............................................................................................34 2.20 Program
Control.....................................................................................................35 2.20.1
The if statement ...............................................................................................35
2.20.2 The switch statement
........................................................................................35 2.20.3 The while
statement..........................................................................................37 2.20.4 The do
statement .............................................................................................37 2.20.5 The
for statement .............................................................................................37 2.20.6
The foreach statement .....................................................................................38 2.21
Console I/O ...........................................................................................................38
2.21.1 Console
Input...................................................................................................38 2.21.2
Console Output.................................................................................................38
2.22 Array
types............................................................................................................38 2.23
Calling methods ref / out ......................................................................................43
-2002, Study Notes. All Rights Reserved. iii
C# - CONSOLE APPLICATIONS
2.23.1 Method Overloading
..........................................................................................44 2.23.2 Variable Method
Parameters - params .................................................................45 2.24 Handling
Exceptions ...............................................................................................45 2.25
Namespaces ..........................................................................................................48
2.26 Namespaces and
Assemblies....................................................................................51 2.27 Summary of
Key Concepts.......................................................................................52
3.
Object
Oriented
Concepts
......................................................................................... 56 3.1 What is an Object?
...................................................................................................56 3.2 The Benefit of
Encapsulation......................................................................................57 3.3 What are
Messages?.................................................................................................57
3.4
What
are
Classes?....................................................................................................58
3.5
What
is
Inheritance?.................................................................................................59
4. Class and
Object....................................................................................................... 61 4.1
Class Declaration
.....................................................................................................61 4.1.1 Class
modifiers...................................................................................................62 4.1.1.1
Abstract classes ........................................................................................63 4.1.1.2
Sealed classes...........................................................................................63 4.1.1.3
Inner Classes ...............................................................................................64 4.1.2
Class base specification.......................................................................................65
4.1.2.1 Base classes..............................................................................................65
4.1.2.2 Internal class ............................................................................................66
4.1.2.3 Interface implementations
.............................................................................66 4.1.3 Class
body.........................................................................................................66 4.1.3.1
Class members .............................................................................................67
4.1.3.2
Signature.....................................................................................................68
4.1.3.3
Constructors.................................................................................................68
4.1.3.4 Calling Base Class Constructors
......................................................................71 4.1.3.5 Static
Constructors........................................................................................71 4.1.3.6
Destructors ..................................................................................................73
4.1.3.7 Inheritance Single.......................................................................................74 4.1.3.8 Accessing
Base Class Members .......................................................................74 4.1.3.9 The this
Reference.......................................................................................75 4.1.3.10 The
Table of Contents
5.3.3 Copying and Deleting
Files...................................................................................92 5.3.4 Reading Text Files
..............................................................................................92 5.3.5 Writing Text
Files................................................................................................93 5.3.6 Reading
Binary Files............................................................................................93 5.3.7
Writing Binary Files.............................................................................................93
5.4
Networking..............................................................................................................9
4 5.4.1
HTTP.................................................................................................................94
5.4.2 Generic Request/Response
Architecture.................................................................95
-2002, Study Notes. All Rights Reserved. v
Chapter 1 Introduction
1. Introduction
1.1 A New Platform?
The crash of the dotcoms and the downturn in the prospects of Java notwithstanding,
Microsofts .NET and C# opens a whole New World of possibilities. There are many people in
the industry, which see a great future to the .NET. We now have .NET, a new and exciting
technology that finally addresses issues weve been grappling with for years (for example,
multi-language development environments, the deployment and versioning problems of
large, complex systems, and so on).
C# - CONSOLE APPLICATIONS
Here are some other sites related to C# http://msdn.microsoft.com/net/
http://www.thecodechannel.com/
http://www.csharphelp.com/
http://www.brinkster.com/Forums/
http://www.c-sharpcorner.com/
http://www.csharpindex.com/
http://www.csharp-station.com/
http://www.c-point.com
http://www.devx.com/dotnet/resources/
http://www.aspwire.com
http://www.asptoday.com
1.7 My Workshops on C#
Here are some brief details of the various workshops on the C# language Console Applications
ADO.NET
WinForms
WebForms
ASP.NET
Web Services
Window / Web Controls and Components
1.9 Acknowledgements
These Study Notes would not have been possible without the contribution, support and
generous help of many individuals.
I would like to acknowledge the help, right from my Java days, to my student and friend
Amit Karmakar, who always believes in me and inspires me to go on to greater heights.
Amit is based in Sydney, Australia and is a Web Developer for the Department of Education
and Training, in New South Wales, Australia.
Sunil Kelkar, who has always stood by me and helped me in going through these notes and
suggest changes. Sunil, is an independent consultant and works in Java and C#, in Pune.
-2002, Study Notes. All Rights Reserved.
Chapter 1 Introduction
I want to thank Rahul Waghmare, who made me look at and explore C# ! Rahul, is a
graphics artist by profession; works in Pune and likes to experiment with various computer
languages Java, VC++, C#.
To all my students and people on the Internet who provided me with information/feedback,
a big thank-you.
I acknowledge all the authors, whose works I have referred. Here is a partial list.
The C# Programming book from Wrox Press was the first book I read on C#. I have been
highly influenced by this book and it probably reflects in these Study Notes.
Ben Albahari, author of C# Essentials for clarifying my doubt on byte array. The book gives
a very precise and to the point description of every element in the language.
Tom Archer, author of Inside C# for his valuable tips and help in clarifying many of my
doubts in C#. His book helped me understand the internal architecture of C#.
Anders Hejlsberg and Scott Wiltamuth for their excellent work, C# Language Reference.
Ashish Banerjee, Willy Denoyette, Sunil Gudipati, Shafeen Sarangi, Chris Maunder, Mahesh
Chand, Sudhakar Jalli, Pramod Singh and Saurabh Nandu for their excellent articles on various
aspects of C#.
Finally to Microsoft Corporation for giving us this C# language.
-2002, Study Notes. All Rights Reserved. 9
C# - CONSOLE APPLICATIONS
2. C# Program Elements
C# (pronounced "C sharp") is a simple, modern, object oriented, and type-safe (a reference
(when not null) is always guaranteed to point to an object that is of the type specified and
that has already been allocated on the heap. Also take note of the fact that a reference can
be null) programming language derived from C and C++. C# aims to combine the high
productivity of Visual Basic (C# is a Rapid Application Development Language like Visual
Basic. It supports the Drag- Drop and Build features of Visual Basic), the elegance of Java
and the raw power of C++.