C# and The .NET Framework

quiverlickforkSoftware and s/w Development

Nov 2, 2013 (4 years and 8 months ago)


C# and The .NET
Week 1
Lesson Overview
•What is .NET?
•The Common Language Runtime
•Microsoft Intermediate Language
•Assemblies and The Common Type
•.NET Supported Languages
•C# .NET
Lesson Overview (Cont.)
•Similarities to Other Languages
•Syntax –Semantics and Rules of the
•Visual Studio 2005
•Solutions, Projects and Items
•Basic C# Project Types
•Console Applications
•Hello World Example
What is .NET?
•.NET is a development and execution environment that
allows different programming languages & libraries to
work together seamlessly to create easy to build, deploy
and maintain applications
•Major Components Include:
–.NET Framework Class Library
–Common Language Runtime (CLR)
–Common Language Specification (CLS)
–Common Type System (CTS)
.NET Framework Class
•The core set of classes that form the basis
for the .NET development environment
•Responsible for exposing the CLR to all
CLS-compliant code
Common Language Runtime
•The CLR is a code execution environment
•All CLS compliant code runs through the
CLR for JIT-compiling
•Access to the CLR is controlled by the
.NET Framework Class Library
Common Language Spec
•Defines what a .NET-compliant language
must provide to the system
•Assures that code meets .NET
specifications and requirements
Microsoft Intermediate
•Languages that target the CLR are
compiled to MSIL
•MSIL is JIT-compiled at run time
•All CLS-compliant (.NET) languages are fist
compiled into MSIL
–Eg. A Visual Basic application and a C#
application with equate to the same IL code
Microsoft Intermediate
Language (Cont.)
•Here is an example of IL code:
Assemblies and The Common
Type System
•Assemblies are logical units of code
•Physically, they are dllor exe files
•Assemblies can be reused/distributed
•Assemblies have a unique versioning
–This avoids DLL Hell!!!
Assemblies and The Common
Type System
•By default all projects in Visual Studio when
compiled will build assemblies
•Assemblies come in two flavours:
–Private Assemblies
–Shared Assemblies
Private Assemblies
•Assemblies that will be used in a single
–Or an assembly could be copied for use in
other applications
–A windows application would be a private
assembly because it will only be a part of one
Shared Assemblies
•Shared Assemblies can be shared by many
•These assemblies would be put in the Global
Assembly Cache (GAC)
–A credit card validation routine could be placed in a
shared assembly and used by many applications
(instead of rewriting the routine in every application)
The Common Type System
•Provides every language running on the
.NET platform with a base set of data types
•Each .NET language is free to implement
aliases for CTS types
•An integer in the CTS is represented as
•C# creates an alias called ‘int’
The Common Type System
•All of the data types in the CTS are
inherited from System.Object
.NET Supported Languages
•Currently there are four languages shipped
with Visual Studio that are CLS-compliant:
–Visual Basic .NET
–Visual C# .NET
–Visual J# .NET
–Visual C++ .NET
•Developers are free to implement their own
CLS-compliant language
Visual C# .NET
•Programming language designed to build a
wide range of applications using the .NET
•Evolved from C and C++
•Simple, Modern and Type Safe
•Completely Object-Orientated
Similarities to Other
•Based on C and C++, C# shares some
common ground with its predecessors
–General Syntax (Grammar)
–Coding Styles and Standards
•With Java in mind, C# was also developed
to make an easy transition from Java to C#
–Several similarities were introduced
C# .NET Syntax
•C# code is typically stored in one or more
text files with the extension .cs
•If we had a file called hello.cswe could
compile that code into hello.exeusing a
command line compiler or as we’ll use in
this class…Visual Studio
C# .NET Syntax -Statements •A statement is a piece of code that
accomplishes a single, simple task
•Print ‘Hello’in a window
•Declare a variable
–Usually statements are 1 line
•All statements must end with a semicolon ‘;’
C# .NET Syntax -Blocks •Code blocks are pieces of code used to
accomplish one or more tasks
–Blocks consist of two or more statements
string msg= “Hello World”;
C# .NET Syntax -General •Within Visual Studio, reserved words will be
shown in blue
•Variable names cannot begin with
–@ # % ( ) { } ; : . ”’[0-9] + -/ \[ ] ^ & *
•Everything is CASE SENSITIVE
–I.e: Hello, hElloand hello are not equal
Visual Studio 2005
•Visual Studio is an Integrated Development
Environment (IDE) for creating .NET
•It is a very powerful IDE that greatly
reduces the number of lines of code that
have to be written manually
Visual Studio 2005
•Throughout this course will be using VS
2005 to develop windows and web
applications in C#.NET 2.0
Solutions, Projects and Items
•Applications within Visual Studio are called
•Projects can contain many different types
of objects which are collectively referred to
as Project Items
•Most applications consist of more than one
Solutions, Projects and Items
•When an application requires more than
one project, you will need to create a
solution (Solutions can have many projects
contained within them)
•Normally a solution is created for every
project to avoid confusion
•Items in one project are exclusive to that
project and are not available in other
projects within a solution
Basic C# Project Types
•There are several types of projects you can
create in Visual Studio. We will only use a
subset of them in this class. This class will
focus on:
–Console Application
–Windows Application
–Class Library
–ASP.NET Website
–And possibly, ASP.NET Web Service
Basic C# Project Types
•Console Application
–This will create an application that will run in a
command line interface. The compiled file will
be an exe file
•Windows Application
–This will create an application that uses
windows to interact with the user (most
common). The compiled file will be an exe.
Basic C# Project Types
•Class Library
–This will create a Dynamically Linked Library
(DLL) file for use within other applications
•ASP.NET Website
–This will create a .NET website project. Use
this project type to create dynamic websites
using ASP.NET
Console Applications
•For the first three weeks of this course we
will be creating only console applications
•This is so that we can focus on the C#
language and not get carried away with
complex or fancy UI design
Console Applications
•Console Applications run within a
command prompt as shown below:
Streams for Communication
•Every language provides a means for input
and output
•In console applications this is achieved by
using streams
•A Stream refers to any input source
(keyboard, mouse) or output destination
(screen, printer) of data
Console Output Stream
•Console Applications can print information
to the command prompt by using the
Console object
•By calling certain functions of this Console
object we can write characters to the
command window
Output Methods
•The methods for used for writing strings to
the command prompt are:
•Used to write some text to the screen
•Used to write some text to the screen as well as a
new line character –“\n”
Console Input Stream
•Console Applications need to accept input
from the user in order to interact with them
•This can be accomplished by using the
Read and ReadLinefunctions of the
Console object
–Console.ReadLine(); //this will read the text
entered by the user
Input Methods
•The methods used to accept input are:
•Reads the next character entered by the user
•Reads the next character or function key entered by
the user
•Reads everything entered by the user from the last
carriage return (enter key)
Hello World Example
•Add Visual Studio to the Start Menu
–As shown in class
•Open Visual Studio 2005
•Create a New Project:
–Under Project Types select Visual C#-
–From Templates Choose “Console Application”
•Diagram on next slide
Hello World Example
Hello World Example
•Click OK. You will see the following:
Hello World Example
•Notice the line that reads
–static void Main(string[] args)
•The application lives between those
–When the code there is done, the application
Hello World Example
•Between the brackets add the line:
•Now press F5 to compile the application
and run it
•Did you see it?
Hello World Example
•The reason that the screen disappeared is
because the code within the brackets was
•We said write this to the screen...it did, and
then shut the application down.
•To prevent this unwanted shutdown, we
could add the folllowingline after the write
Hello World Example
•Now the application will wait until you press
a key before shutting down