C# and The .NET Framework

quiverlickforkSoftware and s/w Development

Nov 2, 2013 (3 years and 10 months ago)

79 views

C# and The .NET
Framework
Week 1
Lesson Overview
•What is .NET?
•The Common Language Runtime
•Microsoft Intermediate Language
•Assemblies and The Common Type
System
•.NET Supported Languages
•C# .NET
Lesson Overview (Cont.)
•Similarities to Other Languages
•Syntax –Semantics and Rules of the
Language
•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
Library
•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
Language
•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
system
–major.minor.build.revision
–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
application
–Or an assembly could be copied for use in
other applications
•Example:
–A windows application would be a private
assembly because it will only be a part of one
application
Shared Assemblies
•Shared Assemblies can be shared by many
applications
•These assemblies would be put in the Global
Assembly Cache (GAC)
•Example:
–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
–Example:
•An integer in the CTS is represented as
System.Int32
•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
Framework
•Evolved from C and C++
•Simple, Modern and Type Safe
•Completely Object-Orientated
Similarities to Other
Languages
•Based on C and C++, C# shares some
common ground with its predecessors
including:
–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
–Examples
•Print ‘Hello’in a window
•Declare a variable
–Usually statements are 1 line
•All statements must end with a semicolon ‘;’
–Console.WriteLine(“Hello”);
C# .NET Syntax -Blocks •Code blocks are pieces of code used to
accomplish one or more tasks
–Blocks consist of two or more statements
•Example
string msg= “Hello World”;
Console.WriteLine(msg);
Console.Read();
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
applications
•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
•Projects can contain many different types
of objects which are collectively referred to
as Project Items
•Most applications consist of more than one
project
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
•Eg
–Console.Write(“HelloUser”);
Output Methods
•The methods for used for writing strings to
the command prompt are:
–Console.Write(stringwhatToWrite)
•Used to write some text to the screen
–Console.WriteLine(stringwhatToWrite)
•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
•Eg
–Console.ReadLine(); //this will read the text
entered by the user
Input Methods
•The methods used to accept input are:
–Console.Read()
•Reads the next character entered by the user
–Console.ReadKey()
•Reads the next character or function key entered by
the user
–Console.ReadLine()
•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#-
Windows
–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
brackets
–When the code there is done, the application
closes
Hello World Example
•Between the brackets add the line:
–Console.Write(“HelloWorld”);
•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
done.
•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
statement
–Console.Read();
Hello World Example
•Now the application will wait until you press
a key before shutting down