1 Presentation1
1 Presentation1
Net
Text Book:
C# Programming : From problem Analysis to program design, Barbara
Doyle, II edition, Cengage learning.
Programming Microsoft® Visual C#® 2008: The Language 2nd Edition –
Donis Marshall
References :
Pro C# with .NET 3.0, Andrew Troelsen, Special Edition, Dream tech
Press, India, 2007.
Inside C#, Tom Archer, WP Publishers, 2001.
The Complete Reference C#, Herbert Schildt, Tata McGraw Hill, 2004.
Programming in C++,E Balagurusamy, A Primer Second Edition,TMH
2009
• With the introduction of the .NET framework,
Microsoft included a new language called C#
(pronounced C Sharp).
• C# is designed to be a simple, modern,
general-purpose, object-oriented programming
language, borrowing key concepts from several
other languages, most notably Java.
• C# could theoretically be compiled to machine
code, but in real life, it's always used in combination
with the .NET framework.
• Therefore, applications written in C#, requires the
.NET framework to be installed on the computer
running the application.
• While the .NET framework makes it possible to
use a wide range of languages, C# is
sometimes referred to as THE .NET language,
perhaps because it was designed together
with the framework.
• C# is an Object Oriented language and does
not offer global variables or functions.
• Everything is wrapped in classes, even simple
types like int and string, which inherits from
the System.Object class.
•
• OOP is based on three key principles :
– Encapsulation (binds together code and data)
– Inheritance (mechanism where one class can
inherit the functionality of another
– Polymorphism (defines one interface that
describes a general set of actions)
Definitions
• An object is an instance of a class
• An object has identity, state and behaviour
• A C# program is basically a collection of
classes.
• A class is defined by a set of declaration
statements and methods containing
instructions known as executable statements.
Difference between Object and Class
• A class is a template for creating an object
• An object is an instance of a class
• An object has memory and reference
• C# can be written with any text editor, like
Windows Notepad, and then compiled with
the C# Command line compiler, csc.exe, which
comes with the .NET framework.
• However, most people prefer to use an IDE
(Integrated Development Environment), and
Microsoft offers several options for this.
• Their flagship is Visual Studio, which can be used
to work on every possible aspect of the .NET
framework.
• This product is very advanced, and comes in
several editions.
• Visual Studio is not exactly cheap, and might even
be too advanced for hobby programmers.
• With .NET framework 2.0, Microsoft introduced
the so-called Express versions, targeted at hobby
programmers and people wanting to try .NET, and
they continued this tradition with the later release
of .NET 3.0 and 3.5.
• For C# programming, you should download the
Visual C# Express from
http://www.microsoft.com/express/download/.
.NET Framework
• .NET represents an advanced new generation
of software that will drive the Next Generation
Internet
• It’s purpose is to make information available
any time, any place, and on any device
• .NET is a software framework that includes
everything required for developing software
for web services
• It integrates presentation technologies,
component technologies and data
technologies on a single platform so as to
enable users to develop internet applications
as easily as they do on desktop systems
• .NET platform provides a new environment for
creating and running robust, scalable and
distributed applications over the web
• .NET platform has three distinct technologies
– Common Language Runtime
– Framework base classes
– User and program Interfaces (ASP.NET and
Winforms)
Characteristics of C#
• Simple – C# simplifies C++ by eliminating
operations such as ->, :: and pointers
• Consistent- C# supports unified type system
• Modern- modern approach to debugging
• Object-oriented – supports encapsulation,
Inheritance and polymorphism
Applications of C#
• Console application
• Windows application
• Developing windows controls
• Developing ASP.NET projects
• Creating web controls
• Providing web services
• Developing .NET component library
A Demonstration of Visual C# 2008,
• If you have ever learnt a programming
language, you know that they all start with the
"Hello, world!" example, and who are we to
break such a fine tradition?
• Start Visual C# Express,
• select File -> New project…
• From the project dialog, select the Console
application.
• Once you click Ok, Visual C# Express creates a
new project for you, including a file called
Program.cs.
using System;
class Hello
{
static void Main()
{
Console.WriteLine(“Hello, World”);
}
}
• C# is a case-sensitive language
• Like Java, C# inherits its basic syntax from C
and C++
– Syntactic punctuation (such as ;, {})
– features (such as case sensitivity)
– keywords (such as void, class, public)
Compiling and Running the application
• The default file extension for C# programs is .cs
– Eg., hello.cs
• Try running the application by pushing F5 on your
keyboard.
• Or it can be compiled with the command line
directive
– csc hello.cs
• This will produce an executable program named
hello.exe
Escape sequences
• Special backslash character constants that are
used in output methods
• \a - alert
• \b - back space
• \f - form feed
• \n - new line
• \r- carriage return
• \t- horizontal tab
• \v - vertical tab
• \”- single quote
• \”” - double quote
• \\- back slash
• \0 - null
The code of your first application
1. using System;
2. namespace Prg2
3. {
4. class Program
5. {
6. static void Main()
7. { Console.WriteLine("Hello, world!");
8. Console.ReadLine();
9. }
10. }
11. }
• Once again, hit F5 to run it, and you will see
the black window actually staying, and even
displaying our greeting to the world.
• The first line tells the compiler that this program uses types from the
System namespace
• Line 2 declares a new namespace, called Prg2
– The new namespace starts at the open curly brace on line 3 and extends through
the matching curly brace on line 11
– Any types declared within this section are members of the namespace
• Line 4 declares a new class type called Program
– Any members declared between the matching curly braces on line 5 and 10 are
members that make up this class.
• Line 6 declares a method called Main as a member of class Program
– Main is a special function used by the compiler as the starting point of the
program
• Line 8 constitutes the body of Main
– Simple Statement are terminated by semi-colon.
– The first statement uses the Console class in namespace System to output a line
of text, and the second one reads a line of text from the console.
Hello world explained
class Program
• We can have more classes, even in the same file.
• For now, we only need one class.
• A class can contain several variables, properties
and methods, concepts we will go deeper into
later on.
• For now, all you need to know is that our current
class only contains one method and nothing else.
• It's declared like this: