ppt - Computer Science and Engineering

advertisement
Getting Started with C#
Chapter 1
1
Objectives
You will be able to:
1.
Say in general terms how C# differs from C.
2.
Create, compile, and run a very simple Windows
console application in C# using Visual Studio 2008.
2
Introduction to C#
C# is an extension of ANSI C
Similar to Java
Microsoft Specific (Sort of)
Technically an ECMA Standard
Adds strong typing.
Adds object oriented programming constructs.
Classes, Inheritance, Polymorphism
Takes away “dangerous” or error prone features.
Pointers, malloc, explicit memory deallocation
No concept of “memory address”
“References” serve the purpose of pointers.
Provides automatic, transparent garbage collection
Lots of useful code in libraries
3
Introduction to C#
Targeted to Microsoft platforms



Specifically to the .NET framework
PC applications for Windows
Web applications for Microsoft web servers
Both GUI and console applications are possible.
An open source, cross-platform implementation
of .NET Framework, Mono, is available:
http://www.mono-project.com
4
Introduction to C#
Supported by the Visual Studio IDE

Very easy to do simple GUI applications



Comparable to Visual Basic
Possible to do large complex PC applications
We will use Visual Studio 2008


Available on all College of Engineering and CSE lab
and classroom computers.
Free download available for USF students.

https://www.dreamspark.com
5
Introduction to C#
A Quick Look at Programming in C#
(Changes and additions to standard C)

Start with Console applications.




Run from a command window (No GUI)
Simplest possible C# programs
Get started with object oriented programming
Later look at Windows Forms applications


GUI Based
A little more complex
6
First C# Program

Let's do "Hello, World!" in C#
7
Start Visual Studio 2008
Your Start Page will look different.
8
Create a New Project
9
Select Project Attributes
10
Template for a Console Application
Generated automatically by Visual Studio
11
The Hello World Program
12
Build the Project
13
Run the Program
Click here to run
14
The Hello World Program
15
Possibly Unfamiliar Features
Overview
(Details to Follow)

Namespace

using ... ;

class Program

static void Main (string[] args)
16
Namespace

Helps avoid name conflicts in large projects

Not necessary, or particularly helpful, on small projects

Visual Studio automatically wraps everything with
namespace projectName
{
}

OK to delete if you wish.
Or just ignore

Be careful when reusing source code.
17
using System;

Recall the line:
Console.WriteLine("Hello, World!");


“Console” is a class in the System namespace
To refer to something outside the current namespace,
you normally have to prefix the name with the
namespace:
System.Console.WriteLine("Hello, World!");

“using System;” permits us to not do this.

C# programs typically begin with lots of “using” statements
18
"using" Statements


We could delete the other three "using"
statements.
Included in the template by Visual Studio
because they are frequently needed

But not needed by this program.
19
using System

If we delete "using System" we get a compile
time error.

Compiler does not know about class Console.
20
Editor Shows Errors
Notice squiggly underlines.
21
using System

If we prefix Console with the namespace
System, there is no error.
namespace Hello
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello, World!");
System.Console.ReadLine();
// Keep window open
}
}
}

"using System" saves typing and clutter.
22
Things to Notice

In C# everything is in a class.


No significance in this simple console app.



Unlike C and C++
Very important in real programs.
Think of “class Program” as a wrapper required
by the environment.
We will use C# classes later in the course.

Similar to classes in C++ and Java.
23
Things to Notice

Every C# program must have a “Main”
static void Main(string[ ] args)


Note capitalization.
“static” means that the function is associated with the
class as a whole rather than with a specific object.



Like C++.
We never instantiate class Program.
“string[ ] args” permits arguments to be passed in from
the command line as in standard C.


Not normally used for Windows applications.
OK to ignore. (Just leave the parentheses empty)
24
Things to Notice
Like standard C and C++
C# is case sensitive
System.Console.Writeline("Hello, World!");
gets a compile error
static void main()
doesn’t work
25
Things to Notice
Like standard C++
the characters // say that the rest of the line is a comment.
Traditional C comments also work.
/* This is a traditional C comment */
26
Running Outside the IDE

We have an executable file: Hello.exe
27
Path to the Executable
28
Double Click the .exe File
29
Deploying the Program
Hello.exe is a complete executable program.


Called an assembly (Microsoft terminology)
MSIL







Microsoft Intermediate Language, not machine code
“Managed code”
Runs under the .NET Common Language Runtime (CLR)
Compiled into machine code as needed. (JIT Compiler)
Can be moved to another directory
Can be copied to another Windows system that
has the .NET Framework installed
Can be run from the command line in a Windows
console window
30
From a Console Window
Copy Hello.exe to C:
Open Command Prompt window and cd to C:
31
Assignment
Before next class:


Read Chapter 1.
Do the examples from this class for
yourself.
32
Download