Module 2:
Overview of C#
By
SRIRAM . B
Overview
Introduction to C# Programming
Features of C# Programming
Structure of a C# Program
Basic Input/Output Operations
Recommended Practices
Compiling, Running, and Debugging
ILDASM Demo
Introduction to C# Programming
C-Sharp is the latest programming language introduced
by Microsoft to overcome the shortcomings of C and C+
+.
The language incorporates the best features of all the
existing programming languages.
It is a language used to create applications for the
latest Microsoft .NET platform.
The Microsoft .NET platform uses small executable units
as the basic building blocks, which can be used for
developing Web-based business solutions.
These units are independent of the operating system
and the programming language used.
Features of C# Programming
The main features of C-Sharp language
are that it:
Is simple
Is consistent
Is scalable
Is object-oriented
Is Web-compatible
Provides automatic garbage collection
Has built-in support for versioning
Architecture of C#
Source Code (.cs)
C# Compiler (csc.exe)
MSIL (.IL file)
Native code/Libraries
JIT Compiler
Executable File
C# Program Execution
1. Application code is written using a .NET
compatible language such as C# :
C# Code
C# Program Execution..
2. This code is compiled into MSIL
(Microsoft Intermediate Language),
which is stored in assembly :
C# application Assembly
Code Compilation
C# Program Execution..
3. When the code is executed, it must first
be compiled into native code using JIT
compiler:
Assembly JIT Compilation Native Code
C# Program Execution..
The Native code is executed in the
context of Managed CLR, along with
any other running application or
process.
System Runtime
System Runtime
Native Code Native Code
Structure of a C# Program
Hello, World
The Class
The Main Method
The using Directive and the
System Namespace
Demonstration: Using Visual
Studio to Create
a C# Program
Hello, World
using
using System;
System;
class
class Hello
Hello
{{
public
public static
static void
void Main()
Main()
{{
Console.WriteLine("Hello,
Console.WriteLine("Hello, World");
World");
}}
}}
The Class
A C# application is a collection of classes,
structures, and types
A class Is a set of data and methods
Syntax
class
class name
name
{{
...
...
}}
A C# application can consist of many files
A class cannot span multiple files
The Main Method
When writing Main, you should:
Use an uppercase “M”, as in “Main”
Designate one Main as the entry point
to the program
Declare Main as public static void
Main
Multiple classes can have a Main
When Main finishes, or returns, the
application quits
The using Directive and the System
Namespace
The .NET Framework provides many
utility classes
Organized into namespaces
System is the most commonly used
namespace
System.Console.WriteLine("Hello,
Refer to classes by their World");
System.Console.WriteLine("Hello, namespace
World");
using
using System;
System;
The using directive
……
Console.WriteLine("Hello,
Console.WriteLine("Hello, World");
World");
Basic Input/Output Operations
The Console Class
Write and WriteLine Methods
Read and ReadLine Methods
The Console Class
Provides access to the standard input,
standard output, and standard error
streams
Only meaningful for console
applications
Standard input – keyboard
Standard output – screen
Standard error – screen
All streams may be redirected
Write and WriteLine Methods
Console.Write and Console.WriteLine
display information on the console
screen
WriteLine outputs a line feed/carriage
return
Both methods are overloaded
A format string and parameters can be
used
Text formatting
Numeric formatting
Read and ReadLine Methods
Console.Read and Console.ReadLine
read user input
Read reads the next character
ReadLine reads the entire input line
Recommended Practices
Commenting Applications
Generating XML Documentation
Exception Handling
Commenting Applications
Comments are important
A well-commented application permits a
developer to fully understand the
structure of the application
Single-line comments
//
// Get
Get the
the user’s
user’s name
name
Console.WriteLine("What
Console.WriteLine("What is is your
your name?
name? ");
");
name
name == Console.ReadLine(
Console.ReadLine( );
);
Multiple-line comments
/*
/* Find
Find the
the higher
higher root
root of
of the
the
quadratic
quadratic equation
equation */
*/
xx == (…);
(…);
Generating XML Documentation
///
/// <summary>
<summary> The
The Hello
Hello class
class prints
prints aa greeting
greeting
///
/// on
on the
the screen
screen
///
/// </summary>
</summary>
class
class Hello
Hello
{{
///
/// <remarks>
<remarks> We
We use
use console-based
console-based I/O.
I/O.
///
/// For
For more
more information
information about
about WriteLine,
WriteLine, see
see
///
/// <seealso
<seealso cref="System.Console.WriteLine"/>
cref="System.Console.WriteLine"/>
///
/// </remarks>
</remarks>
public
public static
static void
void Main(
Main( ))
{{
Console.WriteLine("Hello,
Console.WriteLine("Hello, World");
World");
}}
}}
Exception Handling
using
using System;
System;
public
public class
class Hello
Hello
{{
public
public static
static void
void Main(string[
Main(string[ ]] args)
args)
{{
try{
try{
Console.WriteLine(args[0]);
Console.WriteLine(args[0]);
}}
catch
catch (Exception
(Exception e)
e) {{
Console.WriteLine("Exception
Console.WriteLine("Exception atat
{0}", e.StackTrace);
{0}", e.StackTrace);
}}
}}
}}
Invoking the Compiler
Compiling from the Command Line
Compiling from Visual Studio
Locating Errors
Running the Application
Running from the Command Line
Type the name of the application
Running from Visual Studio
Click Start Without Debugging on the
Debug menu
Debugging
Exceptions and JIT Debugging
The Visual Studio Debugger
Setting breakpoints and watches
Stepping through code
Examining and modifying variables
ILDASM Demo
Go to Visual Studio Command Prompt
Change to your C# project directory
E.g E:\C#\Hello\Bin\Debug folder
Type ILDASM hello.exe , it will display a window
with tree structure
Expand the “hello node”
Expand the “helloIt node”
On the view Menu, click show Source Lines
Double click the “Main Method”
Double click the “Manifest”
Close ILDASM window
Overview of C# Flashback
Structure of a C# Program
Basic Input/Output Operations
Recommended Practices
Compiling, Running, and Debugging
ILDASM Demo
Session Ends
Exercise
Relax