Você está na página 1de 16

C# - Console Applications

Study Notes
Owner: Satish Talim
File: C# - Console Applications
Last saved: 7/6/2001
Email Id. medunet@vsnl.com
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

new modifier ....................................................................................75 4.1.3.11


Casting between Types ................................................................................76
4.1.3.12 Access modifiers
.........................................................................................77 4.1.3.13 Restrictions on
Using Accessibility Levels........................................................78 4.1.3.14 Accessibility
Domain ....................................................................................78 4.1.3.15 Virtual
methods.......................................................................................79 4.1.3.16 Override
methods....................................................................................80 4.2 Interfaces
...............................................................................................................81 4.3
Structs....................................................................................................................8
3 4.4 Enums
....................................................................................................................83 4.5
Properties
...............................................................................................................84 4.6
Assignment
.............................................................................................................85 4.7
Summary of Key Concepts
........................................................................................88
5. Using .NET Base Classes
........................................................................................... 90 5.1 The WinCV Tool
.......................................................................................................90 5.2
StringBuilder
class....................................................................................................90 5.3 File and
Folder operations .........................................................................................91 5.3.1
Finding out information about a File......................................................................91
5.3.2 Listing Files in a
Folder........................................................................................91
-2002, Study Notes. All Rights Reserved.

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).

1.2 System Requirements


To build and run your C# programs, you will need Windows 2000, IE 5.5, Microsoft .NET
SDK (Beta 1), an optional Visual Studio .NET (available for MSDN Universal Subscribers)
and an optional free C# IDE Sharp Develop.
The
C#
IDE
is
available
from
http://www.icsharpcode.net/OpenSource/SD/Download/default.asp . The .NET SDK can be
downloaded from http://msdn.microsoft.com/net Its an 80MB download. The C# compiler
(csc.exe) currently ships with the .NET beta. Do not get confused by this, because as of
today we have the Beta 1 version of .NET, the PDC version and the pre Beta 2 version.
The use of Visual Studio.NET has been intentionally avoided, to focus on the language and
runtime environment and not place any restrictions on your particular development
environment. Therefore, all the sample codes in these notes will compile and run from the
command line.

1.3 Purpose of these Study Notes


To learn the C# language, I had to refer to all available material books, articles on the
net, discussion groups. As I studied the C# language, I kept making notes. I felt the need of
a single repository where individuals could go to and know everything about a particular
topic in C# this repository became the Study Notes. Please appreciate, that this is not a
book on C#.

1.4 Who can use these Study Notes?


These Study Notes supplement my C# lectures and assume a basic knowledge of Java.
However, I am confident that it would prove useful to all those interested in learning the C#
language.

1.5 Updates to this document


The latest version of this document in .pdf format is available at the URL:
http://www.pune-csharp.com/downloads/index.htm
All feedback / suggestions about this document can be sent to medunet@vsnl.com

1.6 Recommended Sites on C#


Amit Karmakar (based in Sydney, Australia) and I have launched a site
http://www.pune-csharp.com

which will cater to all the technical needs of the C# professionals.


-2002, Study Notes. All Rights Reserved. 7

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.8 Satish Talim?


Is a software professional with over 23 years of software consulting experience. He has
been working on Java-based software-export projects and corporate training in Java since
1995. His clients include Satyam Ltd., KPIT, IBM ACE, Focus Inc., Kanbay Software Ltd.,
Global Electronic Commerce Services amongst many others. He is also a certified instructor
at the Sun Authorized Java Training center in Pune. He was the founder Director of Infonox
Software Pvt. Ltd. (ISPL) based in California, USA. His current area of interest is C#.

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++.

2.1 Overview of the .NET


.NET shifts the focus in computing from a world in which individual devices and Web sites
are simply connected through the Internet to one in which devices, services, and computers
work together to provide richer solutions for users.
The .NET infrastructure comprises the .NET Framework, Microsoft Visual Studio.NET, the
.NET Enterprise Servers, and Microsoft Windows.NET. The .NET infrastructure refers to all
the technologies that make up the new environment for creating and running robust,
scalable, distributed applications. The part of .NET that lets us develop these applications is
the .NET Framework.
The .NET Framework consists of the:
Common Language Runtime (CLR). Think of the CLR as the virtual machine / runtime
environment in which .NET applications function. The code that runs within the CLR runs in
an encapsulated and managed environment, separate from other processes on the machine.
.NET Framework class libraries sometimes called the Base Class Library (BCL). All .NET
languages have the .NET Framework class libraries at their disposal. The .NET Framework
class libraries include support for everything from file I/O and database I/O to XML and
SOAP. The .NET Framework class libraries are very vast. The BCLs functionality is available
to all languages that use the CLR.
universal type system called the .NET Common Type System (CTS). In addition to defining
all types, the (CTS) also stipulates the rules that the CLR follows with regard to applications
declaring and using these types. In the world of .NET and C# everything in the CTS is an
object. In fact, not only is everything an object but, even more importantly, all objects
implicitly derive from a single base class defined as part of the CTS. This base class called
System.Object.
NGWS software development kit defines a "Common Language Subset" (CLS), that
ensures seamless interoperability between CLS-compliant languages and class libraries. For
C# developers, this means that even though C# is a new language, it has complete access
to the same rich class libraries that are used by seasoned tools such as Visual Basic and
Visual C++. C# itself does not include a class library. Because the same .NET base class
library is shared between all programming languages, a developer can take his knowledge
of this library with him as he migrates from language to language.

C# is a programming language for developing applications for the Microsofts .NET


development platform. C# is provided as a part of Microsoft .NET. In addition to C#, .NET
supports Visual Basic, Visual C++, and the scripting languages VBScript and Jscript (infact
21 languages so far). All of these languages provide access to the Next Generation Windows
Services (NGWS) platform (or the new Microsoft .NET platform). A developer picks the .NET
language that he/she likes most, writes components in it, and shares the compiled binary
versions of his/her components
-2002, Study Notes. All Rights Reserved.

Você também pode gostar