11 getting started with c# chapter 1. 22 objectives you will be able to: 1. say in general terms how...
TRANSCRIPT
22
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.
33
Introduction to C#
C# is an extension of ANSI CSimilar to JavaMicrosoft 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 deallocationNo concept of “memory address”“References” serve the purpose of pointers.
Provides automatic, transparent garbage collection
Lots of useful code in libraries
44
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
55
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
66
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
1616
Possibly Unfamiliar Features
Overview(Details to Follow)
Namespace
using ... ;
class Program
static void Main (string[] args)
1717
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.
1818
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
19
"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.
20
using System If we delete "using System" we get a
compile time error. Compiler does not know about class Console.
22
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.
2323
Things to Notice
In C# everything is in a class. Unlike C and C++
No significance in this simple console app. 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.
2424
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)
static void Main()
2525
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
2626
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 */
30
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